Main Tutorials

Java 8 BiConsumer Examples

In Java 8, BiConsumer is a functional interface; it takes two arguments and returns nothing.


@FunctionalInterface
public interface BiConsumer<T, U> {
  void accept(T t, U u);
}

Further ReadingJava 8 Consumer Examples

1. BiConsumer

JavaBiConsumer1.java

package com.mkyong.java8;

import java.util.function.Consumer;

public class JavaBiConsumer1 {

    public static void main(String[] args) {

      BiConsumer<Integer, Integer> addTwo = (x, y) -> System.out.println(x + y);
      addTwo.accept(1, 2);    // 3

    }

}

Output


3

2. Higher Order Function

2.1 This example accepts BiConsumer as an argument, create a generic addTwo to join two objects.

JavaBiConsumer2.java

package com.mkyong.java8;

import java.util.function.BiConsumer;

public class JavaBiConsumer2 {

    public static void main(String[] args) {

        addTwo(1, 2, (x, y) -> System.out.println(x + y));          // 3
        addTwo("Node", ".js", (x, y) -> System.out.println(x + y)); // Node.js

    }

    static <T> void addTwo(T a1, T a2, BiConsumer<T, T> c) {
        c.accept(a1, a2);
    }

}

Output


3
Node.js

2.2 More BiConsumer examples.

JavaBiConsumer3.java

package com.mkyong.java8;

import java.util.function.BiConsumer;

public class JavaBiConsumer3 {

    public static void main(String[] args) {

        math(1, 1, (x, y) -> System.out.println(x + y));   // 2
        math(1, 1, (x, y) -> System.out.println(x - y));   // 0
        math(1, 1, (x, y) -> System.out.println(x * y));   // 1
        math(1, 1, (x, y) -> System.out.println(x / y));   // 1

    }

    static <Integer> void math(Integer a1, Integer a2, BiConsumer<Integer, Integer> c) {
        c.accept(a1, a2);
    }

}

Output


2
0
1
1

3. Map.forEach

In the JDK source code, Map.forEach accepts a BiConsumer as an argument.

Map.java

default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
                k = entry.getKey();
                v = entry.getValue();
            } catch (IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            action.accept(k, v);
        }
    }
JavaMapBiConsumer.java

package com.mkyong.java8;

import java.util.LinkedHashMap;
import java.util.Map;

public class JavaMapBiConsumer {

    public static void main(String[] args) {

        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 + ":" + v));

    }

}

Output


1:Java
2:C++
3:Rust
4:JavaScript
5:Go

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments