What is the Consumer functional interface in Java?
Consumer is a functional interface that accepts an argument and returns no result.
The interface contains two methods:
accept()andThen()
The Consumer interface is defined in the java.util.function package.
Use the following statement to import the Consumer interface.
import java.util.function.Consumer;
1. accept()
This method accepts a single input and performs the given operation on the input without returning any result.
Syntax
void accept(T t)
Parameter
T t: The input argument.
Return value
The method doesn’t return any result.
Code
import java.util.function.*;public class Main{public static void main(String[] args) {// Implementation of Consumer interface that consumes and prints the passed valueConsumer<String> consumer = (t) -> System.out.println("The passed parameter is - " + t);String parameter = "hello-educative";// calling the accept method to execute the print operationconsumer.accept(parameter);}}
In the code above, we created an implementation of the Consumer interface that prints the passed argument to the console.
2. andThen()
This method is used to chain multiple Consumer interface implementations one after another. The method returns a composed consumer of different implementations defined in the order. If the evaluation of any Consumer implementation along the chain throws an exception, it is relayed to the caller of the composed function.
Syntax
default Consumer<T> andThen(Consumer<? super T> after)
Parameter
Consumer<? super T> after: The next implementation of the consumer to evaluate.
Return value
The method returns a composed Consumer that performs in sequence the defined operation, followed by the after operation.
Code
import java.util.Arrays;import java.util.List;import java.util.function.Consumer;public class Main{static class Student{public int age;public String name;public Student(int age, String name) {this.age = age;this.name = name;}@Overridepublic String toString() {return "Student{" +"age=" + age +", name='" + name + '\'' +'}';}}public static void main(String[] args) {Consumer<Student> consumer1 = s1 -> s1.age += 1;Consumer<Student> consumer2 = System.out::println;List<Student> studentList = Arrays.asList(new Student(18, "andy"), new Student(19, "jack"), new Student(20, "Dan"));System.out.println("Before chaining:");studentList.forEach(consumer2);System.out.println("----");System.out.println("After chaining:");studentList.forEach(consumer1.andThen(consumer2));}}
Explanation
- From line 1 to line 3, we import the relevant packages.
- In line 5, we define the
Mainclass. - From line 7 to line 22, we define the
Studentclass withnameandageas the attributes of that class. We define theconstructorand thetoString()method of the class. - In line 25, we define the main method of the
Mainclass. - In line 26, we define the first consumer, i.e.,
consumer1, which accepts aStudentobject and increases the age of theStudentby one. - In line 27, we define the second consumer i.e.,
consumer2, which accepts aStudentobject and prints it to the console. - In line 28, we define a list of
Studentobjects with different values fornameandage. - In line 30, we print the
Studentobjects defined in line 28 using theforEach()that accepts a consumer implementation. - In line 33, we chain the
consumer2toconsumer1using theandThen()method onconsumer1object.