Java 8 BiConsumer Example

In this tutorial, we will learn how to use Java 8 BiConsumer interface with example.
The BiConsumer Interface is a functional interface introduced in Java 8 and we use the lambda expression to implement Java 8 BiConsumer interface.
Learn functional interfaces at https://www.javaguides.net/2018/07/java-8-functional-interfaces.html
Learn lambda expressions at https://www.javaguides.net/2018/07/java-8-lambda-expressions.html.

Video

Java 8 BiConsumer Interface Overview

java.util.function.BiConsumer is a functional interface whose functional method is accept(T t,U u). The BiConsumer interface represents an operation that takes two arguments (T,U) and returns no result.
This how the interface is defined:
@FunctionalInterface
public interface BiConsumer<T, U> {
     void accept(T t, U u);
     // andThen default method is defined
}

Java 8 - BiConsumer Example

Note the comments are self-descriptive.
package com.javaguides.java.functionalinterfaces;

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


public class BiConsumerDemo {

    public static void main(String[] args) {

        // Here's an example using an anonymous class:
        BiConsumer < Integer, Integer > consumerObj = new BiConsumer < Integer, Integer > () {

            @Override
            public void accept(Integer a, Integer b) {
                System.out.println(a + b);

            }
        };

        consumerObj.accept(10, 20);


        // And with a lambda expression:
        BiConsumer < Integer, Integer > biConsumer = (a, b) -> System.out.println(a + b);

        BiConsumer < Integer, Integer > substraction = (a, b) -> System.out.println(a - b);

        BiConsumer < Integer, Integer > multiplication = (a, b) -> System.out.println(a * b);

        BiConsumer < Integer, Integer > division = (a, b) -> System.out.println(a / b);

        biConsumer.accept(10, 20);

        substraction.accept(20, 10);

        multiplication.accept(10, 50);

        division.accept(20, 10);


        Map < Integer, String > map = new LinkedHashMap < > ();
        map.put(1, "Java");
        map.put(2, "C++");
        map.put(3, "Rust");
        map.put(4, "JavaScript");
        map.put(5, "Go");

        map.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });
    }
}
Output:
30
30
10
500
2
1
Java
2
C++
3
Rust
4
JavaScript
5
Go

BiConsumer.and() Method

This interface also has the following default method:
default BiConsumer<T, U> andThen(
             BiConsumer<? super T, ? super U> after)
This method returns a composed BiConsumer that performs, in sequence, the operation of the consumer followed by the operation of the parameter.
As in the case of a Consumer, these methods are useful to combine BiConsumers and make the code more readable, for example:
BiConsumer<String, String> first = (t, u) -> System.out.println(t.toUpperCase() + u.toUpperCase());
BiConsumer<String, String> second = (t, u) -> System.out.println(t.toLowerCase() + u.toLowerCase());
first.andThen(second).accept("Again", " and again");
The output is:
AGAIN AND AGAIN again and again

Related Java 8 Tutorials:

Comments