Consumer Interface is another Built-In Functional Interface of java.util.function package of Java 8. The consumer interface consumes the object and doesn’t return any object. it has only one abstract method called accept(). Along with this, it has one more default method called andThen()
void accept(T t) // abstract method
default Consumer andThen(Consumer super T> after)) // default method
Example for Consumer Interface
public class ConsumerExample
{
public static void main(String[] args) {
Consumer msg = a -> System.out.println(a);
msg.accept("Hello World");
}
}
Output
Hello World
andThen() method of Consumer Interface
andThen() method is a default method of Consumer Function Interface. andThen() method accepts a consumer interface as a parameter called after which to be applied after the current one.
Below is the example to square each number and print of a list:
public class ConsumerExample
{
public static void main(String[] args) {
//Multiply by 2 each Integer element
Consumer> doubleValue = a -> {
for(int i=0; i> printValue = a ->{
a.stream().forEach(p->System.out.println(p));
};
List values = new ArrayList<>(Arrays.asList(3,4,5));
doubleValue.andThen(printValue).accept(values);
}
}
Output
6
8
10
Conclusion
In this article, we have seen consumer interfaces with some examples.


Java 8: Introduction to Streams API
Java 8 got a brand new API called streams. it makes every developer’s life easy. Stream APIs are used to process the group of data. mostly streams are applied on collection objects but we can use stream with any group

Java 8: Consumer – Built-In Functional Interfaces
Consumer Interface is another Built-In Functional Interface of java.util.function package of Java 8. The consumer interface consumes the object and doesn’t return any object. it has only one abstract method called accept(). Along with this, it has one more default

Java 8: Supplier – Built-In Functional Interfaces
Supplier Interface is another Built-In Functional Interface of java.util.function package of Java 8. Supplier Interface used for assignment purposes. It has only one abstract method and doesn’t have any default or static method. Below is the get() method signature. It

Java 8: Predicate – Built-In Functional Interfaces
Predicate Interface is another Built-In Functional Interface of java.util.function package of Java 8. Predicate Interface can be used to evaluate the expression which returns the boolean result like true or false of the given expression. Example: Predicate logic = X

Java 8: Built-In Functional Interfaces. Supplier, Function, Predicate
Functional Interface was added in Java 8. Functional Interface is like a normal interface with only one abstract method. Remember, a Functional interface can have a default method but should have only one abstract method which is unimplemented. Also, @FunctionalInterface