In this post, I will show how to use BiConsumer functional interface with an example.
BiConsumer functional interface is similar to Consumer functional interface. It takes an input, processes it and doesn’t return anything.
The difference lies in the number of inputs taken by the BiConsumer functional interface.
Consumer functional interface takes one input whereas BiConsumer functional interface takes two inputs instead of one.
BiConsumer functional interface has abstract “accept” method which takes the two input.
The class implementing the BiConsumer functional interface has to provide the logic in the accept method.
Below is the compelete example
Main Class
1 package function;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.function.BiConsumer;
6
7 public class BiConsumerDemo1 {
8 public static void main(String[] args) {
9 List<TataEmployee> tataEmployees = new ArrayList<TataEmployee>();
10 tataEmployees.add(new TataEmployee("name1", false, 2019));
11 tataEmployees.add(new TataEmployee("name2", false, 2018));
12 tataEmployees.add(new TataEmployee("name3", false, 2017));
13 tataEmployees.add(new TataEmployee("name4", false, 2016));
14 tataEmployees.add(new TataEmployee("name5", false, 2015));
15 tataEmployees.add(new TataEmployee("name6", false, 2014));
16 tataEmployees.add(new TataEmployee("name7", false, 2013));
17
18 CustomBiConsumerFunction customBiConsumerFunction = new CustomBiConsumerFunction();
19 for(TataEmployee tataEmployee : tataEmployees) {
20 customBiConsumerFunction.accept(tataEmployee, 2017);
21 System.out.println(tataEmployee);
22 }
23 }
24 }
25
26 class CustomBiConsumerFunction implements BiConsumer<TataEmployee, Integer> {
27 @Override
28 public void accept(TataEmployee tataEmployee, Integer yearOfTermination) {
29 if(tataEmployee.getYearOfJoining() >= yearOfTermination) {
30 tataEmployee.setTerminated(true);
31 }
32 }
33 }
34
35 class TataEmployee {
36 private String name;
37 private boolean terminated;
38 private int yearOfJoining;
39
40 public TataEmployee(String name, boolean terminated, int yearOfJoining) {
41 super();
42 this.name = name;
43 this.terminated = terminated;
44 this.yearOfJoining = yearOfJoining;
45 }
46
47 public String getName() {
48 return name;
49 }
50
51 public void setName(String name) {
52 this.name = name;
53 }
54
55 public boolean isTerminated() {
56 return terminated;
57 }
58
59 public void setTerminated(boolean terminated) {
60 this.terminated = terminated;
61 }
62
63 public int getYearOfJoining() {
64 return yearOfJoining;
65 }
66
67 public void setYearOfJoining(int yearOfJoining) {
68 this.yearOfJoining = yearOfJoining;
69 }
70 @Override
71 public String toString() {
72 return name + "-->" + terminated + "-->" + yearOfJoining;
73 }
74 }
The above code goes through a list of Tata Employees and terminate employees who have joined after “yearOfTermination”.
The Tata Employees are represented by class TataEmployee.
The logic that terminates employees using an employee instance and “yearOfTermination” is in class “CustomBiConsumerFunction” which implements the interface
“BiConsumer” as shown from line 26 to 33. The accept function compares each employees yearOfJoining with yearOfTermination. If yearOfJoining is after the yearOfTermination, the employee will be terminated otherwise left unchanged.
In the main method from line 9 to 16 we come up with a list of employees.
At line 18, we create an instance of CustomBiConsumerFunction named customBiConsumerFunction.
From line 19 to line 22, we loop through the list and for every employee we call accept method passing the employee and yearOfTermination.
In this way we can use the BiConsumer functional interface.