BiFunction Functional Interface in Java

BiFunction interface in Java is an extension of the Function interface

It has one functional (single abstract) method R apply(T t, U u), which takes two input parameters of type T and U and returns the function result of type R.

Since it is a Functional Interface, we can implement it with a Lambda expression.

BiFunction<T, U, R>

@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);

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     */
    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}


T and U represent the types of input parameters, and R represents the return type.

It also contains one default method <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) that takes a Function as a parameter and returns a composed function that first applies this function to its input, and then applies the after function to the result. 

Implementing the BiFunction interface

Example 1:

Implementation of the single abstract method R apply(T t, U u)

class Test {

  public static void main(String[] args) {
    BiFunction<String, String, Integer> sumLengthsOfBothStrings = (str1, str2) -> str1.length() + str2.length();
    System.out.println("The sum of the lengths is: " + sumLengthsOfBothStrings.apply("alegru", "coding"));
  }
}
Output: The sum of the lengths is: 12

Here, inside the braces <>, we specified what inputs and output types and used a Lambda expression to write the implementation of the BiFunction interface.

Between the parentheses (), we specified the inputs, and on the right side of the arrow sign (->), we wrote the logic which will return the sum of the lengths of the input strings.

NoteIf we have a single statement inside the Lambda body, then we don’t need to put the curly braces {}.
Also, if we have a single statement that returns some result, we don’t have to put the return keyword. It would be redundant.

Example 2:

Implementation of the <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after), which we already mentioned above.

class Test {

  public static void main(String[] args) {
    BiFunction<String, String, Integer> sumLengthsOfBothStrings = (str1, str2) -> str1.length() + str2.length();
    Function<Integer, Integer> doubleTheNumber = (num) -> num * 2;
        
    System.out.println(sumLengthsOfBothStrings.andThen(doubleTheNumber).apply("alegru", "coding"));
  }
}
Output: 24

Here, we wrote the program to calculate the sum of the input strings lengths and then multiply the sum by 2.

So, using the andThen() method, we chained the method’s calls, and first sumLengthsOfBothStrings() got executed, then the DoubleTheNumber() method.

The andThen() method must be followed by the apply() method to pass the parameters.

That was all about the BiFunction interface in Java. Proceed to the next lesson.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *