Java 8 – BiFunction Functional Interface

In this article, we will discuss pre-defined Functional Interface in Java 1.8 version for performing operation based on 2 input arguments and returning results after computation/processing i.e.; BiFunction Functional Interface

1. BiFunction Functional Interface:

  • This Functional Interface has one abstract method called R apply(T t, U u); which
  • Step 1 :- accepts 2 input arguments of any data-type
  • Step 2 :- performs some operation on the 2 input arguments
  • Step 3 :- And return result of any data-type
  • Below is the BiFunction Functional interface containing apply(); method along with other default & static methods
  • Note: BiFunction is exactly same as that of Function Functional Interface except that it accepts 2 input arguments whereas Function accepts single argument
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface BiFunction<T, U, R> {

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);
    
    // other default and static methods
}

2. Examples for BiFunction Functional Interface:

Example 1 – program to create Student Object based on input arguments supplied/passed to lambda expression

package net.bench.resources.bifunction.example;

import java.util.function.BiFunction;

class Student {

	// member variables
	String name;
	int rollno;

	// public constructor
	public Student(String name, int rollno) {
		super();
		this.name = name;
		this.rollno = rollno;
	}
}

public class CreateStudentObject {

	public static void main(String[] args) {

		// lambda expression to create Student object using BiFunction FI
		BiFunction<String, Integer, Student> bf = (name, rollno) -> new Student(name, rollno);

		// create Student object by invoking above lambda expression
		Student s = bf.apply("Ashwin", 1002);

		// printing student info
		System.out.println("Name     : " + s.name);
		System.out.println("Roll No. : " + s.rollno);
	}
}

Output:

Name     : Ashwin
Roll No. : 1002

Example 2 – program to create a ArrayList of Student Objects

package net.bench.resources.bifunction.example;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

class Student {

	// member variables
	String name;
	int rollno;

	// public constructor
	public Student(String name, int rollno) {
		super();
		this.name = name;
		this.rollno = rollno;
	}
}

public class ListOfStudentObjects {

	public static void main(String[] args) {

		// lambda expression to create Student object using BiFunction FI
		BiFunction<String, Integer, Student> bf = (name, rollno) -> new Student(name, rollno);

		// create ArrayList object to store of Student objects
		List<Student> students = new ArrayList<>();

		// add Student object to List by invoking above lambda expression
		students.add(bf.apply("Warne", 1001));
		students.add(bf.apply("Murali", 1002));
		students.add(bf.apply("Kumble", 1003));
		students.add(bf.apply("Vettori", 1004));
		students.add(bf.apply("Saqlain", 1005));

		// printing student info by iterating
		for(Student student : students) {

			// print info
			System.out.println("Name : " + student.name + "\t Roll No. : " + student.rollno);
		}
	}
}

Output:

Name : Warne	 Roll No. : 1001
Name : Murali	 Roll No. : 1002
Name : Kumble	 Roll No. : 1003
Name : Vettori	 Roll No. : 1004
Name : Saqlain	 Roll No. : 1005

3. Difference between Function and BiFunction Functional Interface :

  • Function :- It accepts 1 input argument and does some operation on the input argument passed and return value in any data-type based on the operation implemented in lambda expression
  • BiFunction :- Whereas it accepts 2 input arguments and does some operation on the 2 input arguments passed and return value in any data-type based on the operation implemented in lambda expression
  • So, it is simple if we want to perform some operation based on single input argument and return value then go for Function Functional Interface
  • But in case if we require to do some operation on 2 input arguments and return value then BiFunction Functional Interface is the best option
package net.bench.resources.bifunction.example;

import java.util.function.BiFunction;
import java.util.function.Function;

public class DifferenceInFunctionAndBiFunction {

	public static void main(String[] args) {

		// 1. Function - lambda expression to square the input value
		Function<Integer, Integer> f = (i) -> i*i;

		// 2. BiFunction - lambda expression to add both input arguments
		BiFunction<Integer, Integer, Integer> bf = (i,j) -> (i+j);

		// 1.A test for Function
		System.out.println("Function testing : \n");

		System.out.println("1. Square of Integer 8 is \t: " + f.apply(8));

		System.out.println("2. Square of Integer 19 is \t: " + f.apply(19));

		// 2.A test for BiFunction
		System.out.println("\nBiFunction testing : \n");

		System.out.println("1. Sum of (10, 6) is \t: " + bf.apply(10, 6));

		System.out.println("2. Sum of (7, 3) is \t: " + bf.apply(7, 3));
	}
}

Output:

Function testing : 

1. Square of Integer 8 is 	: 64
2. Square of Integer 19 is 	: 361

BiFunction testing : 

1. Sum of (10, 6) is 	: 16
2. Sum of (7, 3) is 	: 10

References:

Happy Coding !!
Happy Learning !!

Java 8 - BiConsumer Functional Interface
Java 8 - BiPredicate Functional Interface