What is the BiFunction in Java 8

A normal Function is a predefined functional interface that takes one input argument and produces a result. If we give two arguments to the Function it will not accept the reason Java people introduced BiFunction functional interface. This BiFunction accepts two arguments and produces a result. It is introduced in Java 8. This is present in the java.util.function package.
Prototype:

public interface BiFunction<T, U, R>{
public R apply(T t);
}

Type Parameters:
T – It is the type of the first argument to the function
U – It is the type of the second argument to the function
R – It is the type of the result of the function
Methods:
   1. R apply(T t, U u) – This method applies this function to the given arguments and produces the result
   2. default BiFunction andThen(Function after) – This method returns a composed function that first applies this function to its input, and then applies the after function to the result
Example:

import java.util.function.BiFunction;

public class Test {
  public static void main(String[] args) {
    BiFunction < Integer, Integer, Integer > bi = (a, b) -> a + b;
    System.out.println("Sum = " + bi.apply(5, 5));
    System.out.println("Sum = " + bi.apply(10, 10));
    BiFunction < Integer, Integer, Integer > bi1 = (a, b) -> a * b;
    System.out.println("Multiply = " + bi1.apply(5, 5));
    System.out.println("Multiply  = " + bi1.apply(10, 10));
  }
}

Output: 

Sum = 10
Sum = 20
Multiply = 25
Multiply  = 100

Example of BiFunction(andThen(Function after)) method:

import java.util.function.BiFunction;

public class Test {

public static void main(String[] args) {

BiFunction < Integer, Integer,Integer > bi1 = (a,b) -> a+b ;

bi1=bi1.andThen(a->a*3);

System.out.println("Result of bi1 ="+bi1.apply(2,3));

System.out.println("Result of bi1 ="+bi1.apply(5,5));

BiFunction < Integer, Integer,Integer > bi2 = (a,b) -> a+b ;

bi2=bi2.andThen(a->a*4);

System.out.println("Result of bi2 ="+bi2.apply(2,3));

System.out.println("Result of bi2 ="+bi2.apply(5,5));
   }
}

Output: 

Result of bi1 =15
Result of bi1 =30
Result of bi2 =20
Result of bi2 =40

Example of BiFunction with Student class object input argument:

import java.util.function.BiFunction;

class Student {

  String name;

  int id;

  public Student(String name, int id) {

    this.name = name;

    this.id = id;

  }

}

public class Test {

  public static void main(String[] args) {

    BiFunction < String, Integer, Student > bi = (name, id) -> new Student(name, id);

    Student stdnt = bi.apply("Raj", 101);

    // printing student detail

    System.out.println("Name: " + stdnt.name);

    System.out.println("Id  : " + stdnt.id);

  }
}

Output: 

Name: Raj
Id  : 101

Example of ArrayList of Student with BiFunction:

import java.util.function.BiFunction;

import java.util.ArrayList;

import java.util.List;

class Student {

  String name;

  int id;

  public Student(String name, int id) {

    this.name = name;

    this.id = id;

  }

}

public class Test {

  public static void main(String[] args) {

    BiFunction < String, Integer, Student > bi = (name, id) -> new Student(name, id);

    List < Student > stndtList = new ArrayList < > ();

    stndtList.add(bi.apply("Raj", 101));

    stndtList.add(bi.apply("Ramesh", 102));

    stndtList.add(bi.apply("Raghu", 103));

    stndtList.add(bi.apply("Ram", 104));

    stndtList.add(bi.apply("Rakesh", 105));

    System.out.println("Student Detail List:");

    for (Student stdnt: stndtList) {

      System.out.println("Name: " + stdnt.name + "\t Id: " + stdnt.id);
     }
   }
}

Output: 

Student Detail List:
Name: Raj Id: 101
Name: Ramesh Id: 102
Name: Raghu Id: 103
Name: Ram Id: 104
Name: Rakesh Id: 105

Conclusion:
This topic is explained What is a BiFunction interface? What are the methods of it? How to use it?

Leave a Comment