What is the BiFunction functional interface in Java?
BiFunction is a functional interface, which accepts two arguments and returns a result. The interface contains two methods:
- The
apply()method - The
andThen()method
The BiFunction interface is defined in the java.util.function package. To import the BiFunction interface, we use the following statement:
import java.util.function.BiFunction;
1. Apply(T t, U u)
The apply() method applies the function to the given arguments and returns the result of that function. This is the functional method of the interface.
Syntax
R apply(T t, U u)
Parameters
T t: This is the first argument.U u: This is the second argument.
Return value
This method returns the result of the function.
Code
In the code written below, we create a BiFunction interface that accepts two string arguments and returns the result of the concatenation of the two strings:
import java.util.function.BiFunction;public class Main{public static void main(String[] args) {// This implementation concats the argument strings passed as parametersBiFunction<String, String, String> concatStrings = (s, s2) -> s.concat(s2);String s1 = "hello";String s2 = "-educative";// calling apply method of the BiFunctionSystem.out.println(concatStrings.apply(s1, s2));}}
2. AndThen(Function<? super R, ? extends V> after)
The andThen() method is used to chain together multiple Function functional interface implementations, such that the result of the first implementation becomes the input to the second implementation. This method returns a composed function of different functions defined in the order. If the evaluation of either function throws an exception, it is relayed to the caller of the composed function.
Syntax
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)
Parameters
Function<? super R, ? extends V> after: This is the function that needs to be applied after the current function is applied.
Return value
This method returns a composed function.
Code
import java.util.function.BiFunction;import java.util.function.Function;public class Main{public static void main(String[] args) {BiFunction<String, String, String> concatStrings = (s1, s2) -> s1.concat(s2);Function<String, String> concatConstants = (s1) -> s1.concat("/edpresso");Function<String, String> convertToUpperCase = String::toUpperCase;String s1 = "Hello";String s2 = " educative";BiFunction<String, String, String> stage1 = concatStrings.andThen(concatConstants);BiFunction<String, String, String> stage2 = stage1.andThen(convertToUpperCase);// calling andThen method of the BiFunctionSystem.out.println(stage2.apply(s1, s2));}}
Explanation
In the code written above, we define a BiFunction (concatStrings) and two different Function (concatConstants and convertToUpperCase) implementations.
The composition of the functions is divided into two stages:
- In line 12, we compose the
concatStringsandconcatConstants. The result of this is stored instage1. - In line 13, we compose the
stage1andconvertToUpperCase. The result of this is stored instage2. - In line 16, we call the
applymethod on thestage2composed object.