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:

  1. accept()
  2. 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 value
Consumer<String> consumer = (t) -> System.out.println("The passed parameter is - " + t);
String parameter = "hello-educative";
// calling the accept method to execute the print operation
consumer.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;
}
@Override
public 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 Main class.
  • From line 7 to line 22, we define the Student class with name and age as the attributes of that class. We define the constructor and the toString() method of the class.
  • In line 25, we define the main method of the Main class.
  • In line 26, we define the first consumer, i.e., consumer1, which accepts a Student object and increases the age of the Student by one.
  • In line 27, we define the second consumer i.e., consumer2, which accepts a Student object and prints it to the console.
  • In line 28, we define a list of Student objects with different values for name and age.
  • In line 30, we print the Student objects defined in line 28 using the forEach() that accepts a consumer implementation.
  • In line 33, we chain the consumer2 to consumer1 using the andThen() method on consumer1 object.

Free Resources