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 Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.