GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Functional interface
  • Lambda Expressions
  • Introduction3 min read
  • Variables2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java 8 | Consumer Interface in Java with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

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.

  • Functional interface containing a single abstract method.
  • Commonly used with Lambda Expressions and Streams.
  • Useful for performing side-effect operations.
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

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.