> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# BiFunction Interface
- URL: https://www.javahandbook.com/java-streams-api/bifunction-interface/
- Published: 2026-02-15T09:04:39.000Z
- Updated: 2026-02-15T09:04:39.000Z
- Description: This lesson talks about the third functional interface, which is the BiFunction.
- Author: Gopi Gorantala
- Tags: #docs, #java-streams-api, Functional Interfaces

## What is a BiFunction interface?

The `BiFunction` interface is a functional interface defined in the `java.util.function` package in Java. It represents a function that accepts two arguments of different types and produces a result of another type. The `BiFunction` interface is similar to the `Function` interface, but it takes two input arguments instead of one.

The `BiFunction` interface declares a single abstract method called `apply()`, which takes two arguments of different types and returns a value of the result type. Here's the method signature of the `apply()` method:

**Syntax:**

```java
R apply(T t, U u);

```

The type parameters `T`, `U`, and `R` represent the types of the two input arguments and the result, respectively. They can be any valid Java types, including primitive types and objects.

To use the `BiFunction` interface, you can create an instance of it using a lambda expression or method reference that implements the `apply()` method. Here's an example that demonstrates how to use a `BiFunction` to concatenate two strings:

```java
import java.util.function.BiFunction;

public class BiFunctionExample {
  public static void main(String[] args) {
    BiFunction<String, String, String> concatenateFunction = (str1, str2) -> str1 + str2;

    // Concatenating two strings using the apply() method
    String result = concatenateFunction.apply("Hello, ", "World!");
    System.out.println("Result: " + result);
  }
}
```

In the example, we create a `BiFunction<String, String, String>` using a lambda expression that concatenates two strings using the `+` operator. Then, we call the `apply()` method on the `BiFunction` instance, passing in two strings, and obtain the concatenated result.

The `BiFunction` interface is commonly used in functional programming and provides a way to operate on two input values to produce a result. It is often used in scenarios where a computation or transformation requires two inputs, such as combining data from different sources, applying calculations involving two variables, or performing custom operations on pairs of elements.

Note that the `BiFunction` interface is part of the Java 8 functional interfaces introduced to support lambda expressions and functional programming constructs. It is often used alongside other functional interfaces like `Consumer`, `Predicate`, and `Function` to create complex functional pipelines and perform multi-argument data processing tasks.

## Examples

### Add integers

Let us take a code snippet with a detailed explanation.

```java
import java.util.function.BiFunction;

public class BiFunctionInterface {
  public static void main(String[] args) {
    biFunction(32, 32);
  }

  private static void biFunction(int A, int B) {
    BiFunction<Integer, Integer, Integer> biFunctionAddition = (X, Y) -> X + Y;

    System.out.println(biFunctionAddition.apply(A, B));
  }
}
```

**Explanation**

1. In `BiFunction<Integer, Integer, Integer>`, the first two input types are set to an `Integer`, and the return type is also set to an `Integer` in the declaration.
2. `x`, `y` are considered as two input arguments whose type is already set to `Integer` in the `BiFunction` declaration.

### User-defined/Custom Functional Interface

We can define our own custom functional interfaces.

Let us try to create a simple custom functional interface.

**Syntax:**

```java
R apply(T t, U u, V v, W w);

```

So what are `T`, `U`, `V`, `W`, and `R` in the **QuadFunction** declared above?

- The first two `T`, `U`, `V`, `W` are arguments.
- The last one `R` is the return type of the function.

With this understanding, here is the implementation.

```java
@FunctionalInterface
public interface QuadFunction<T, U, V, W, R> {
    R apply(T t, U u, V v, W w);
}
```

```java
class Main {
  public static void main(String[] args) {
    QuadFunction<Integer, Integer, Integer, Integer, Integer> quadFunction =
        (t, u, v, w) -> t * u + v * w;

    System.out.println(quadFunction.apply(1, 2, 3, 4));
  }
}
```