What is the Java Consumer Interface?

The Java Consumer interface of the java.util.function package is a functional interface that is often used as an assignment target for lambda expressions. accept(T t) is the interface’s single abstract method which takes an object as input and returns nothing. The andThen(Consumer<? super T> after) method provides a simple way to string multiple Consumer objects together by calling accept() first on the object itself and then accept() on the object passed as the parameter.

For more information on functional interfaces in Java click here: What is a Java Functional Interface?.

Consumer Interface Source Code

Consumer Interface Example

Creating the ConsumerExample class by implementing Consumer. The accept(T t) method takes a string and prints it.

Creating a Consumer Instance with Lambda Expression

Using a lambda expression to create an instance of Consumer. The accept(T t) method takes a string and prints it.

Leave a Reply

