Java BiConsumer Interface with examples

 238 total views,  6 views today

Welcome to Java 8 Series. In this tutorial, you will learn about Java BiConsumer with examples.

BIConsumer is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of BiConsumer?

Consider a case where we need to write a lambda expression which accepts two inputs and perform an operation on two inputs and return no result.

Example

 public static void main(String[] args) {
        Operation ops = (String i, String j) -> {
            System.out.println(i);
            System.out.println(j);
        };

        ops.print("aa","bb");
}

interface Operation
{
    void print(String i, String j);
}

When you look at the above example, we have a function interface with an abstract method which accepts two arguments and returns void. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us BIConsumer interface. Let now see in-depth details on Java BIConsumer Interface with examples.

BiConsumer Interface

Interface BiConsumer<T,U>

T – the type of the first argument to the operation
U – the type of the second argument to the operation

BIConsumer interface contains an operation that accepts two input arguments and returns no result.

It is a functional interface and the functional method is accept(Object, Object).

Method Description:

void accept(T t, U u)
Performs this operation on the given arguments.
Parameters:
t – the first input argument and it is a generic type.
u – the second input argument and it is a generic type.

Example of BIConsumer interface using lambda.

BiConsumer<String, String> consumer = (String i, String j) -> {
            System.out.println(i);
            System.out.println(j);
        };
consumer.accept("apple", "orange");

Example of BIConsumer interface using Method reference

public class Test
{
    public static void main(String[] args)
    {
        BiConsumer<String, String> consumer = Test::print;
        consumer.accept("apple", "orange");
    }
 
    static void print(String i,String j) {
        System.out.println(i);
        System.out.println(j);
    }
}

Example of BIConsumer interface in collection

BiConsumer<String, String> consumer = (String i, String j) -> {
            System.out.println(i);
            System.out.println(j);
};

Map<String,String> map = new HashMap<>();
map.put("1", "apple");
map.put("2", "android");
map.forEach(consumer);

If you want to write a lambda expression with two arguments and no result, please use Java BIConsumer interface.

Bi Consumer andThen()

default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after)

It is a default method which accepts a BIConsumer.This will be executed after the current BIConsumer is executed.

Example

BiConsumer<String, String> consumerObj = (String i, String j) -> {
       System.out.println(1);
       System.out.println(2);
};
       
BiConsumer<String, String> consumer = (String i, String j) -> {
       System.out.println(i);
       System.out.println(j);
};           
consumer.andThen(consumerObj).accept("apple", "orange");

References

Java 8 Official documentation for BI Consumer

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

3 thoughts on “Java BiConsumer Interface with examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.