The BiConsumer Interface is a functional interface introduced in Java 8 that represents an operation accepting two input arguments and performing an action without returning any value. It is commonly used when an operation needs to process two related values together, such as a key-value pair or two objects.
- Part of the java.util.function package.
- Contains a single abstract method, accept(T t, U u), making it a functional interface.
- Supports chaining multiple operations using the andThen() default method.
Syntax
@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
...
}
}
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
Method of BiConsumer Interface
There are following methods used in BiConsumer Interface
1. accept()
The accept() method performs an operation using the two supplied arguments.
Syntax:
void accept(T t, U u)
- t : First input argument.
- u : Second input argument.
This method does not return any value.
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class GFG {
public static void main(String args[])
{
// Create the first list
List<Integer> lista = new ArrayList<Integer>();
lista.add(2);
lista.add(1);
lista.add(3);
// Create the second list
List<Integer> listb = new ArrayList<Integer>();
listb.add(2);
listb.add(1);
listb.add(2);
// BiConsumer to compare both lists
BiConsumer<List<Integer>, List<Integer> >
equals = (list1, list2) ->
{
if (list1.size() != list2.size()) {
System.out.println("False");
}
else {
for (int i = 0; i < list1.size(); i++)
if (list1.get(i) != list2.get(i)) {
System.out.println("False");
return;
}
System.out.println("True");
}
};
equals.accept(lista, listb);
}
}
Output
False
2. andThen()
The andThen() method returns a composed BiConsumer that performs the current operation followed by another BiConsumer.
Syntax:
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after)
- after : Another BiConsumer to execute after the current operation.
This interface returns a composed BiConsumer.
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class Main {
public static void main(String args[])
{
// Create first list
List<Integer> lista = new ArrayList<Integer>();
lista.add(2);
lista.add(1);
lista.add(3);
// Create second list
List<Integer> listb = new ArrayList<Integer>();
listb.add(2);
listb.add(1);
listb.add(2);
// BiConsumer to compare 2 lists of integers
BiConsumer<List<Integer>, List<Integer> > equals = (list1, list2) ->
{
if (list1.size() != list2.size()) {
System.out.println("False");
}
else {
for (int i = 0; i < list1.size(); i++)
if (list1.get(i) != list2.get(i)) {
System.out.println("False");
return;
}
System.out.println("True");
}
};
// BiConsumer to print 2 lists
BiConsumer<List<Integer>, List<Integer> > disp = (list1, list2) ->
{
list1.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
list2.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
};
// Using addThen() method
equals.andThen(disp).accept(lista, listb);
}
}
Output
False 2 1 3 2 1 2
andThen() method throws NullPointerException if the after operation is null.
Example To demonstrate when NullPointerException is returned.
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class Main {
public static void main(String args[])
{
// Create first list
List<Integer> lista = new ArrayList<Integer>();
lista.add(2);
lista.add(1);
lista.add(3);
// Create second list
List<Integer> listb = new ArrayList<Integer>();
listb.add(2);
listb.add(1);
listb.add(2);
// BiConsumer to compare 2 lists of integers
BiConsumer<List<Integer>, List<Integer> > equals = (list1, list2) ->
{
if (list1.size() != list2.size()) {
System.out.println("False");
}
else {
for (int i = 0; i < list1.size(); i++)
if (list1.get(i) != list2.get(i)) {
System.out.println("False");
return;
}
System.out.println("True");
}
};
// BiConsumer to print 2 lists
BiConsumer<List<Integer>, List<Integer> > disp = (list1, list2) ->
{
list1.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
list2.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
};
try {
equals.andThen(null).accept(lista, listb);
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
Output
Exception : java.lang.NullPointerException
Example To demonstrate how an Exception in afterThen function is returned and handled.
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class Main {
public static void main(String args[])
{
// Create first list
List<Integer> lista = new ArrayList<Integer>();
lista.add(2);
lista.add(1);
lista.add(3);
// Create second list
List<Integer> listb = new ArrayList<Integer>();
listb.add(2);
listb.add(1);
// BiConsumer to compare 2 lists of integers
BiConsumer<List<Integer>, List<Integer> > equals = (list1, list2) ->
{
for (int i = 0; i < list1.size(); i++)
if (list1.get(i) != list2.get(i)) {
System.out.println("False");
return;
}
System.out.println("True");
};
// BiConsumer to print 2 lists
BiConsumer<List<Integer>, List<Integer> > disp = (list1, list2) ->
{
list1.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
list2.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
};
try {
disp.andThen(equals).accept(lista, listb);
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
Output
2 1 3 2 1 Exception : java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2