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

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">