Java | BiFunction Interface methods - apply() and andThen()

Last Updated : 20 Jun, 2026

The BiFunction<T, U, R> interface is a functional interface introduced in Java 8 and available in the java.util.function package. It represents a function that accepts two input arguments and produces a result. It is commonly used when an operation requires two values as input and returns a single output.

  • Supports lambda expressions and method references.
  • Useful for calculations, transformations, and data processing.
  • Provides apply() and andThen() methods.
Java
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {

        // Calculate total price
        BiFunction<Integer, Integer, Integer> totalPrice =
            (quantity, price) -> quantity * price;

        int result = totalPrice.apply(5, 100);

        System.out.println("Total Price = " + result);
    }
}

Output
Total Price = 500

Explanation: In this example, the BiFunction accepts two integer arguments: quantity and price. The apply() method multiplies these values and returns the total price. For a quantity of 5 and price of 100, the result is 500.

Syntax

@FunctionalInterface
public interface BiFunction<T, U, R>

Here,

  • T : Type of first input argument
  • U : Type of second input argument
  • R : Type of returned result

Methods of BiFunction Interface

1. apply() Method

The apply() method is the functional method of the BiFunction interface. It accepts two arguments, performs the specified operation, and returns the result.

  • Core method of the interface.
  • Accepts two input parameters.

Syntax:

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.

Java
import java.util.function.BiFunction;

public class Main {
    public static void main(String args[])
    {
        // BiFunction to add 2 numbers
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

        // Implement add using apply()
        System.out.println("Sum = " + add.apply(2, 3));

        // BiFunction to multiply 2 numbers
        BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;

        // Implement add using apply()
        System.out.println("Product = " + multiply.apply(2, 3));
    }
}

Output
Sum = 5
Product = 6

Explanation: In this example, the apply() method executes the logic defined in the BiFunction. The first function adds two numbers (2 + 3 = 5), while the second function multiplies them (2 × 3 = 6). The result is returned and printed.

2. andThen() Method

The andThen() method returns a composed BiFunction that first applies the current function and then applies another Function to the result.

  • Executes the current function first.
  • Passes the result to another function.

Syntax:

default <V> BiFunction<T, U, V>
andThen(Function<? super R, ? extends V> after)

  • 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. 
Java
import java.util.function.BiFunction;

public class Main {
    public static void main(String args[])
    {
        // BiFunction to demonstrate composite functions
        // Here it will find the sum of two integers
        // and then return twice their sum
        BiFunction<Integer, Integer, Integer> composite1 = (a, b) -> a + b;

        // Using andThen() method
        composite1 = composite1.andThen(a -> 2 * a);

        // Printing the results
        System.out.println("Composite1 = " + composite1.apply(2, 3));

        // BiFunction to demonstrate composite functions
        // Here it will find the sum of two integers
        // and then return twice their sum
        BiFunction<Integer, Integer, Integer> composite2 = (a, b) -> a * b;

        // Using andThen() method
        composite2 = composite2.andThen(a -> 3 * a);

        // Printing the result
        System.out.println("Composite2 = " + composite2.apply(2, 3));
    }
}

Output
Composite1 = 10
Composite2 = 18

Explanation: Here, andThen() chains another operation after the BiFunction. For composite1, the sum (2 + 3 = 5) is calculated first and then doubled to produce 10. For composite2, the product (2 × 3 = 6) is calculated first and then tripled to produce 18.

Program 1: To demonstrate when NullPointerException is returned. 

Java
import java.util.function.BiFunction;

public class Main {
    public static void main(String args[])
    {
        // BiFunction which finds the sum of 2 integers
        // and returns twice their sum
        BiFunction<Integer, Integer, Integer> composite = (a, b) -> a + b;

        try {
            // Using andThen() method
            composite = composite.andThen(null);

            // Printing the result
            System.out.println("Composite = " + composite.apply(2, 3));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Output
Exception: java.lang.NullPointerException

Explanation: In this example, null is passed to the andThen() method instead of a valid Function. Since andThen() requires a non-null function, Java throws a NullPointerException.

Program 2: To demonstrate how an Exception in the after function is returned and handled. 

Java
import java.util.function.BiFunction;

public class Main {
    public static void main(String args[])
    {
        // BiFunction which finds the sum of 2 integers
        // and returns twice their sum
        BiFunction<;Integer, Integer, Integer> composite = (a, b) -> a + b;

        // Using andThen() method
        composite = composite.andThen(a -> a / (a - 5));

        try {
            // Printing the result using apply()
            System.out.println("Composite = " + composite.apply(2, 3));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Output
Exception: java.lang.ArithmeticException: / by zero

Explanation: The BiFunction first adds 2 and 3, producing 5. This result is then passed to the function inside andThen(), which performs 5 / (5 - 5). Since the denominator becomes 0, an ArithmeticException (/ by zero) is thrown and caught by the catch block.

Note:

  • It isn't possible to add a BiFunction to an existing BiFunction using andThen().
  • 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.

Advantages of BiFunction

  • Accepts two input arguments in a single function.
  • Reduces the need for custom interfaces.
  • Supports lambda expressions and method references.
  • Enables function composition using andThen().
  • Improves code readability and reusability.
  • Useful for calculations, transformations, and data processing
Comment