Java Consumer Interface with examples

 498 total views,  3 views today

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

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

What is the use of Consumer?

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

Example

   public static void main(String[] args) {
        Operation ops = (String i) -> {
            System.out.println(i);
        };
        ops.print("aa");
    }
    interface Operation
    {
        void print(String i);
    }

When you look at the above example, we have a function interface with an abstract method which accepts one argument 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 a Consumer interface.

Let us now learn in-depth details about Java Consumer Interface with examples.

Consumer Interface

Interface Consumer<T>

T - the type of input to the operation

Consumer interface contains an operation that accepts one input argument and returns no result.

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

Method description

void accept(T t)

t - the input argument

Example of Consumer interface using lambda

public static void main(String[] args) {

        Consumer<String> consumer = (String i) -> {
            System.out.println(i);
        };
        consumer.accept("apple");
}

Example of Consumer interface using Method reference

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

Example of Consumer interface in collection

Consumer<String> consumer = Test::print;

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.forEach(consumer);

If you want to write a lambda expression with one argument and no result, please use Java Consumer interface.

Consumer andThen()

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

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

Example

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

Consumer<String> consumerObj = (String i) -> {
    System.out.println(i+"me");
};

consumer.andThen(consumerObj).accept("Raj");

Output

Raj
Rajme

References

Java 8 Consumer Interface official documentation

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!.