Guide to Bifunction Interface in Java

Introduction

In this blog we will learn how to use BiFunction interface in Java with some examples.

In the previous blog we learnt about how to use Function interface which just takes single argument.

BiFunction will take 2 inputs, perform some operations on it and return a result.

BiFunction<T, U, R> functional method is R apply(T t, U u).

1
2
3
4
@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

Let’s get started!

Basic examples

In the first example we will take inputs as integers and return product of it.

In the second example we will take inputs as strings and return length of concatenated string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.examples;

import java.util.List;
import java.util.function.BiFunction;

public class App {

    public static void main(String[] args) {

        BiFunction<Integer,Integer,Integer> multiply = (x,y) -> x*y; 
        System.out.println(multiply.apply(10,20)); // 200

        BiFunction<String,String,Integer> concatandFindLength = (x,y) -> x.concat(y).length();
        System.out.println(concatandFindLength.apply("Hello","World")); // 10

    }
}

Combining BiFunction and Function

We can combine BiFunction and Function in a pipeline using andThen method.

In the below example we have BiFunction which returns the product of 2 numbers.

Output of BiFunction is fed to Function using andThen method.

Function transform the input given into customised output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.examples;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

public class App {

    public static void main(String[] args) {

        BiFunction<Integer,Integer,Integer> add = (x,y) -> x*y;
        Function<Integer,String> output = (x) -> "Product is "+x;
        String result= add.andThen(output).apply(5,10);
        System.out.println(result);  // Product is 50

    }
}

Let us take one more use case.

In the below example we concatenate 2 strings using BiFunction and convert them to uppercase using Function as shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.examples;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

public class App {

    public static void main(String[] args) {

        BiFunction<String,String,String> add = (x,y) -> x.concat(y);
        Function<String,String> output = x -> x.toUpperCase();
        String result= add.andThen(output).apply("This is ", "new code");
        System.out.println(result); // THIS IS NEW CODE

    }
}

Conclusion

BiFunction is similar to Function but it takes 2 arguments. We can compose BiFunction together with Function which gives much flexibility to perform business on the data as it is processed.

Hope this clarifies how to use BiFunction in our code.

Learn more functional interfaces

  1. Function interface
  2. Consumer interface
  3. Predicate interface
  4. BiPredicate interface