Java 8 BiFunction Interface

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Java 8 BiFunction interface.

Java 8 BiFunction Interface

BiFunction Interface :-

BiFunction ( java.util.function.BiFunction ) is a functional interface that has one abstract method and one default method declared in it. It represents an algorithm where two input parameter of same or different type is used to produce return value of same or another type.

Abstract method :- 

R  apply ( T t, U u ) :- The abstract method apply ( T t, U u ) is a functional method. It accepts two parameter of type T andas an argument and return one parameter R as a return value. It represents an algorithm or a business logic, where a passed argument T and U is used to produce a return value R.

Default method :-  

default  <V>  BiFunction<T,U,R>   andThen ( Function <? super R, ? extends V> after ) :- It returns a function that performs two operation in sequence, first the operation of a bifunction on which method is called, this operation will take two parameter of type T  and U as an argument and return one parameter of type R as a return value. Second, the operation of a function passed as an argument in andThen method, this operation will take one parameter of type R ( Generated during first operation as an argument and return one parameter of type V as a return value.

Reference :-  BiFunction Interface JavaDocs 

Java 8 Stream API have one method where BiFunction interface is used as an argument, they are as follows :-

<U> U reduce ( U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner )

There are three specialised ( primitive specific ) form of Java 8 BiFunction interface, which can be used in case of int, long and double. They are as follows :-

  1. ToIntBiFunction<T,U> interface ( JavaDocs ) :- It represents a bifunction that accepts two parameter of type T and U as an argument and returns an integer value.
  2. ToDoubleBiFunction<T,U> interface ( JavaDocs ) :- It represents a bifunction that accepts two parameter of type T and U as an argument and returns a double value.
  3. ToLongBiFunction<T,U> interface ( JavaDocs ) :- It represents a bifunction that accepts two parameter of type T and U as an argument and returns a long value.

Example :- 

This example will show you, how to create and call different methods of a bifunction interface.

  public static void main(String[] args){
		
    // t is designation and u is age, return value is salary
    BiFunction<String,Integer,Integer> bifunction1 = ( t,u ) -> { 
							  if(t != null && u != null) {
							    if(t.equalsIgnoreCase("Manager") && u > 30){
                                                                return 1000000;
                                                            }else if(t.equalsIgnoreCase("Developer") && u > 25){
                                                                return 100000;
                                                            }else {
                                                                return 0;
                                                            }
                                                          }else{
                                                            return 0;
                                                          }
                                                         };
			  
    int salary = bifunction1.apply("Manager",30);
    System.out.println(salary);
		  
    salary = bifunction1.apply("Developer",28);
    System.out.println(salary);
		  
    Function<Integer,String> function2 = ( t ) -> { if(t >= 1000000){
                                                      return "Band 5";
                                                    }else if(t >= 100000){
                                                      return "Band 4";
                                                    }else{
                                                      return "Band 3";
                                                    }
                                                  };
			 
    // Example of andThen method	 
    String band1 = bifunction1.andThen(function2).apply("Manager",30);
    System.out.println(band1);
  } 

Result :- 

0
100000
Band 3

That’s all for Java 8 BiFunction Interface. If you liked it, please share your thoughts in comments section and share it with others too.