![]() |
VOOZH | about |
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:-
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.
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:
Returns: This method does not return any value.
Below is the code to illustrate accept() method:
Program 1:
10 4 2 6
2. andThen()
It returns a composed Consumer 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:
4 2 6
Program 2: To demonstrate when NullPointerException is returned.
Exception: java.lang.NullPointerException
Program 3: To demonstrate how an Exception in the after function is returned and handled.
2 1 3 Exception: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3