Java BiFunction Tutorial

Overview

In Java, A BiFunction represents a function that accepts two arguments and returns a result. The interface has one abstract method apply and one default method andThen.

The apply method is defined as follows:

Java

That means it accepts two arguments (t of type T and u of type U) and returns an object of type R.

After reading this post, you will (hopefully) know the following details:

  • Basic usage of BiFunction
  • How to use BiFunction in your code
  • Some practical use cases

Basic Usage

Let’s consider some common operations with BiFunction.

Creating and calling BiFunction

Java

The code above creates a BiFunction called stringJoiner. As you can see, the types of the arguments and the return object don’t need to be different.

Running the code above would provide the following results:

Running BiFunction

Let’s try something less boring than just String. As you may know, a male Donkey and a female Horse went to a bar and sometime later, they will produce a Mule. However, if a female Donkey and a male Horse went to a bar, the couple would produce a Hinny!

Let’s first create some classes to represent the animals:

Java

And here is the BiFunction

Java

Let’s try with Jack the male horse and Jane the female Donkey.

Java

You can expect that the result is a Hinny:

Applying a BiFunction on different object types

Chaining BiFunction and Function

Imagine you need to write a function to calculate the shipping fee and tax for an order of many items. The steps are:

  1. First, calculate the total (without shipping and tax)
  2. Next, calculate the total with the shipping
  3. Finally, calculate the tax based on the total with shipping

The BiFunction interface has a default method called andThen that accepts a Function. You can achieve the above requirements with the following code:

Java

The andThen method returns a BiFunction. That means you can keep calling andThen to pass in as many Functions as you need.

Running the code would provide the following result:

Chaining BiFunction with Function

Conclusion

In this post, I’ve shown you the basic usages of the BiFunction functional interface in Java. To use this interface, you need to provide the implementation for the apply method. You can also use the andThen method to chain subsequent functions to further process the result.

Leave a Comment