Skip to content

BiFunction Interface

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

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:

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:

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.

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:

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.

@FunctionalInterface
public interface QuadFunction<T, U, V, W, R> {
    R apply(T t, U u, V v, W w);
}
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));
  }
}
Java

Gopi Gorantala Twitter

Gopi is a highly experienced Full Stack developer with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.

Comments


Related Posts

Members Public

How To Write Lambda Expressions

This lesson introduces the basic lambda expression structure with tips to write efficient code.

Members Public

What Are Lambda Expressions?

This is an introductory lesson on lambda expressions. You will learn about the lambda operator, expression, syntaxes and more!

Members Public

Power Of Two (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!