Java 8 Consumer Interface in Java
The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t return any value. The Consumer Interface is present in the java. util package. The Consumer interface takes only one generic argument “T”, it denotes the type of input.
An object of the Consumer type is provided a lambda expression, which is used to construct the accept() method, which finally applies the specified operation to its argument. Since consumers are expected to function through side effects, they are helpful when no value needs to be returned.
Functions used in Consumer Interface:
- accept():
The accept() method accept an argument and executes the operation on specified argument.
Syntax:
void accept(T obj)
The accept() method doesn’t return any value.
- andThen():
The parameterized Consumer will be invoked afterwards the first one in the composed consumer that andThen() method returns. The caller of the composed operation is informed if any errors that are thrown during the evaluation of either function.
Syntax:
default Consumer <T> andThen(Consumer <? super T> after)
Example Program:
// 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 Consumer {
public static void main(String args[])
{
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);
;
}
}
Output:
2 4 6
Example 2:
// 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 Consumer {
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);
}
}
}
Output:
2 1 3 Exception; java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
Summary:
The Consumer Interface is used to implement the functional programming in Java. In the Consumer Interface, it only takes a single argument and return the result. The Consumer Interface is preferred to use only when the function doesn’t return any value.