BiConsumer example in Java 8

BiConsumer is a functional interface that takes two arguments and has no return value.

@FunctionalInterface
public interface BiConsumer<T, U> {

    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     */
    void accept(T t, U u);

}

1. BiConsumer

BiConsumer can be implemented as a Lambda expression. If you pass two arguments with the accept() call, the implementation is executed.

import java.util.function.BiConsumer;

public class BiConsumerExample1 {

    public static void main(String[] args) {

        BiConsumer<Integer, Integer> biConsumer = (n1, n2) -> System.out.println(n1 * n2);

        biConsumer.accept(10, 20);
    }
}

Output:

200

2. High Order Function

BiConsumer can be passed as an argument to a function.

import java.util.function.BiConsumer;

public class BiConsumerExample2 {

    public static void main(String[] args) {

        BiConsumer<Integer, Integer> biConsumer = (n1, n2) -> System.out.println(n1 * n2);

        multiply(10, 20, biConsumer);
    }

    static void multiply(Integer n1, Integer n2, BiConsumer<Integer, Integer> biConsumer) {
        biConsumer.accept(n1, n2);
    }
}

Output:

200

3. Map.forEach()

Map.forEach() takes a BiConsumer as an argument.

You can output all the contents of a Map by passing a BiConsumer like this:

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class BiConsumerExample3 {

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        BiConsumer<String, Integer> biConsumer =
                (key, value) -> System.out.println("key: " + key + ", value: " + value);

        map.forEach(biConsumer);
    }
}

Output:

key: one, value: 1
key: two, value: 2
key: three, value: 3

4. andThen()

andThen() is a chaining method that connects BiConsumers.

When calling biConsumer1.andThen(biConsumer2).accept(10, 20) in the code below, biConsumer1 is executed first, followed by biConsumer2. Two BiConsumers are passed 10 and 20 as arguments.

import java.util.function.BiConsumer;

public class BiConsumerExample4 {

    public static void main(String[] args) {
        BiConsumer<Integer, Integer> biConsumer1 =
                (n1, n2) -> System.out.println("multiply: " + (n1 * n2));

        BiConsumer<Integer, Integer> biConsumer2 =
                (n1, n2) -> System.out.println("sum: " + (n1 + n2));

        biConsumer1.andThen(biConsumer2).accept(10, 20);
    }
}

Output:

multiply: 200
sum: 30

In BiConsumer.java, andThen() is implemented as follows.

// BiConsumer.java

default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
    Objects.requireNonNull(after);

    return (l, r) -> {
        accept(l, r);
        after.accept(l, r);
    };
}

References

codechachaCopyright ©2019 codechacha