The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don’t return any value.
Hence this functional interface which takes in one generic namely:-
- T: denotes the type of the input argument to the operation
The lambda expression assigned to an object of Consumer type is used to define its accept() which eventually applies the given operation on its argument. Consumers are useful when it not needed to return any value as they are expected to operate via side-effects.
Functions in Consumer Interface
The Consumer interface consists of the following two functions:
1. accept()
This method accepts one value and performs the operation on the given argument
Syntax:
void accept(T t)
Parameters: This method takes in one parameter:
- t– the input argument
Returns: This method does not return any value.
Below is the code to illustrate accept() method:
Program 1:
// Java Program to demonstrate // Consumer's accept() method 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); } } |
10 4 2 6
2. andThen()
It returns a composed Comsumer wherein the parameterized Consumer will be executed after the first one. If evaluation of either function throws an error, it is relayed to the caller of the composed operation.
Note: The function being passed as the argument should be of type Consumer.
Syntax:
default Consumer <T>
andThen(Consumer<? super T> after)
Parameters: This method accepts a parameter after which is the Consumer to be applied after the current one.
Return Value: This method returns a composed Consumer that first applies the current Consumer first and then the after operation.
Exception: This method throws NullPointerException if the after operation is null.
Below is the code to illustrate andThen() method:
Program 1:
// Java Program to demonstrate // Consumer's andThen() method 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 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 integers 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); // using addThen() modify.andThen(dispList).accept(list); ; } } |
4 2 6
Program 2: To demonstrate when NullPointerException is returned.
// Java Program to demonstrate // Consumer's andThen() method 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 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 integers 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); try { // using addThen() modify.andThen(null).accept(list); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.NullPointerException
Program 3: To demonstrate how an Exception in the after function is returned and handled.
// Java Program to demonstrate // Consumer's andThen() method 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 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 integers Consumer<List<Integer> > dispList = list -> list.stream().forEach(a -> System.out.print(a + " ")); System.out.println(); List<Integer> list = new ArrayList<Integer>(); list.add(2); list.add(1); list.add(3); // using addThen() try { dispList.andThen(modify).accept(list); ; } catch (Exception e) { System.out.println("Exception: " + e); } } } |
2 1 3 Exception: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Producer-Consumer solution using threads in Java
- Producer-Consumer solution using Semaphores in Java | Set 2
- Understanding threads on Producer Consumer Problem | Java
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Comparator Interface in Java with Examples
- List Interface in Java with Examples
- SortedSet Interface in Java with Examples
- SortedMap Interface in Java with Examples
- Function Interface in Java with Examples
- ToLongFunction Interface in Java with Examples
- LongToIntFunction Interface in Java with Examples
- ToIntFunction Interface in Java with Examples
- ToDoubleFunction Interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

