Java 8 | Consumer Interface in Java with Examples

Last Updated : 1 Oct, 2026

The Consumer Interface is a functional interface available in the java.util.function package, introduced in Java 8. It represents an operation that accepts a single input argument and performs an action on it without returning any result. Consumer is commonly used for operations such as printing values, modifying collections, logging data, and performing side effects.

Java
import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {

        Consumer<String> display =
            name -> System.out.println("Hello, " + name);

        display.accept("John");
    }
}

Output
Hello, John

Explanation: In this example, a Consumer<String> accepts a string value and prints a greeting message. The accept() method passes "John" to the Consumer, which performs the operation without returning any value.

Syntax

@FunctionalInterface
public interface Consumer<T>

T: denotes the type of the input argument to the operation.

Methods of Consumer Interface

The Consumer interface consists of the following two functions:

1. accept() Method

The accept() method accepts a single argument and performs the specified operation on it. Since Consumer is designed for side effects, this method does not return any value.

Syntax: 

void accept(T t)

  • Parameters: This method takes in one parameter: t- the input argument
  • Returns: This method does not return any value.
Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

public class Main {
    public static void main(String args[])
    {
        // Consumer to display a number
        Consumer<Integer> display = a -> System.out.println(a);

        // Implement display using accept()
        display.accept(10);

        // Consumer to multiply 2 to every integer of a list
        Consumer<List<Integer> > modify = list ->
        {
            for (int i = 0; i < list.size(); i++)
                list.set(i, 2 * list.get(i));
        };

        // Consumer to display a list of numbers
        Consumer<List<Integer> >
            dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));

        List<Integer> list = new ArrayList<Integer>();
        list.add(2);
        list.add(1);
        list.add(3);

        // Implement modify using accept()
        modify.accept(list);

        // Implement dispList using accept()
        dispList.accept(list);
    }
}

Output
10
4 2 6

Explanation: In this example, a Consumer is created to display an integer value. When accept(10) is called, the Consumer receives the value 10 and prints it to the console. The Consumer receives a list and doubles each element. The accept() method executes the operation directly on the provided list.

2. andThen() Method

The andThen() method combines two Consumer operations into a single Consumer. The current Consumer executes first, followed by the Consumer passed as an argument.

Syntax:

default Consumer<T> andThen(Consumer<? super T> after)

  • Parameter: after represents the Consumer operation that is executed after the current Consumer.
  • Return Value: Returns a composed Consumer that performs the current operation first and then the after operation.
  • Exception: Throws NullPointerException if the after Consumer is null.

Example: Chaining Consumers with andThen()

Java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Main {
    public static void main(String[] args)
    {

        // Consumer to double each element
        Consumer<List<Integer> > modify = list ->
        {
            for (int i = 0; i < list.size(); i++) {
                list.set(i, 2 * list.get(i));
            }
        };

        // Consumer to display the elements
        Consumer<List<Integer> > display = list ->
        {
            for (int value : list) {
                System.out.print(value + " ");
            }
        };

        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);

        // Chaining Consumers using andThen()
        modify.andThen(display).accept(list);
    }
}

Output
4 2 6 

Explanation: The modify Consumer first doubles each element of the list. After it completes, the display Consumer prints the modified elements. The andThen() method combines these two operations and executes them sequentially.

Comment