The BiFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result.
Hence this functional interface which takes in 3 parameters namely:-
- T: denotes the type of the first argument to the function
- U: denotes the type of the second argument to the function
- R: denotes the return type of the function
The lambda expression assigned to an object of BiFunction type is used to define its apply() which eventually applies the given function on the arguments. The main advantage of using a BiFunction is that it allows us to use 2 input arguments while in function we can only have 1 input argument.
Functions in BiFunction Interface
The BiFunction interface consists of the following two functions:
This method applies the given function to the arguments.
R apply(T t, U u)
Parameters: This method takes two parameters:
- t– the first function argument
- u– the second function argument
Returns: This method returns the function result which is of type R.
Below is the code to illustrate apply() method:
Sum = 5 Product = 6
It returns a composed function wherein the parameterized function will be executed after the first one. If evaluation of either function throws an error, it is relayed to the caller of the composed function.
Note: The function being passed as the argument should be of type Function and not BiFunction.
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)
where V is the type of output of the after function, and of the composed function
Parameters: This method accepts a parameter after which is the function to be applied after this function is one.
Return Value: This method returns a composed function that first applies the current function first and then the after function
Exception: This method throws NullPointerException if the after function is null.
Below is the code to illustrate addThen() method:
Composite1 = 10 Composite2 = 18
Program 2: To demonstrate when NullPointerException is returned.
Program 3: To demonstrate how an Exception in the after function is returned and handled.
In the below program, when (2, 3) is passed as the parameter to the first function, it return the sum 5. Now this sum will be passed as the parameter to the after function, i.e. addThen() method. Here passing 5 to after function results in (5 – 5 = 0), i.e. the denominator will become 0. Hence ArithmeticException will be thrown. This exception will be handled in the apply() function, instead of addThen() function.
Exception: java.lang.ArithmeticException: / by zero
- It isn’t possible to add a BiFunction to an existing BiFunction using addThen().
- BiFunction interface is useful when it is needed to pass 2 parameters, unlike Function interface which allows to pass only one. However, it is possible to cascade two or more Function objects to form a BiFunction but in that case it won’t be possible to use both the parameters at the same time. This is the utility of BiFunction.
- The Lambda expression is being used to initialize the apply() method in BiFunction interface.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.