The BiConsumer 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 two arguments and produces a result. However these kind of functions don’t return any value.
This functional interface takes in two generics, namely:-
- T: denotes the type of the first input argument to the operation
- U: denotes the type of the second input argument to the operation
The lambda expression assigned to an object of BiConsumer type is used to define its accept() which eventually applies the given operation on its arguments.
BiConsumers are useful when it is not required to return any value as they are expected to operate via side-effects.
Functions in BiConsumer Interface
The BiConsumer interface consists of the following two functions:
This method accepts two values and performs the operation on the given arguments
void accept(T t, U u)
Parameters: This method takes in two parameters:
- t– the first input argument
- u– the second input argument
Returns: This method does not return any value.
Below is the code to illustrate accept() method:
Program 1: Program to compare 2 list of integers using BiConsumer’s accept() method:
It returns a composed BiConsumer wherein the parameterized BiConsumer will be executed after the first one. If evaluation of either operation throws an error, it is relayed to the caller of the composed operation.
Note: The operation being passed as the argument should be of type BiConsumer.
default BiConsumer <T, U> andThen(BiConsumer<? super T, ? super U> after)
Parameters: This method accepts a parameter after which is the BiConsumer to be applied after the current one.
Return Value: This method returns a composed BiConsumer that first applies the current operation 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:
False 2 1 3 2 1 2
Program 2: To demonstrate when NullPointerException is returned.
Exceptiion : java.lang.NullPointerException
Program 3: To demonstrate how an Exception in the after function is returned and handled.
2 1 3 2 1 Exceptiion : java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
- Properties forEach(BiConsumer) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- DoubleFunction Interface in Java with Examples
- LongToDoubleFunction Interface in Java with Examples
- List Interface in Java with Examples
- SortedSet Interface in Java with Examples
- SortedMap 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 firstname.lastname@example.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.