Home » Java 8 Consumer Interface

Java 8 Consumer Interface

by Online Tutorials Library

Java Consumer Interface

It is a functional interface defined in java.util.function package. It contains an abstract accept() and a default andThen() method. It can be used as the assignment target for a lambda expression or method reference.

The Consumer Interface accepts a single argument and does not return any result.

Java Consumer Interface Methods

Method Description
void accept(T t) It performs this operation on the given argument.
default Consumer<T> andThen(Consumer<? super T> after) It returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

Java Consumer Interface Example 1

Output:

Hello John 12 

Java Consumer Interface Example2

Output:

Sum of list values: 100 

You may also like