In this post under Java, we will explain with an example the andThen method under BiConsumer functional interface.
BiConsumer’s andThen method is used to chain one BiConsumer interface implementation with one or more BiConsumer interface implementations.
Lets consider an example. I have a list of TCS employees with the class structure as shown below
TCSEmployee
class TCSEmployee {
private String name;
private boolean promoted;
private int yearOfJoining;
private int salary;
public TCSEmployee(String name, boolean promoted, int yearOfJoining, int salary) {
super();
this.name = name;
this.promoted = promoted;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public boolean isPromoted() {
return promoted;
}
public void setPromoted(boolean promoted) {
this.promoted = promoted;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return name + "-->" + promoted + "-->" + yearOfJoining + "-->" + salary;
}
}
I will create two BiConsumer interface implementations named “CustomBiConsumerFunction1” and “CustomBiConsumerFunction2”, both will take TCSEmployee instance and yearOfPromotion as arguments.
One implementation will promote employees whose yearOfJoining will be greater than given yearOfPromotion.
Another implementation will increase the salary of employees whose yearOfJoining will be greater than given yearOfPromotion.
Below are the implementations
CustomBiConsumerFunction1
class CustomBiConsumerFunction1 implements BiConsumer<TCSEmployee, Integer> {
@Override
public void accept(TCSEmployee tcsEmployee, Integer yearOfPromotion) {
if(tcsEmployee.getYearOfJoining() >= yearOfPromotion) {
tcsEmployee.setPromoted(true);
}
}
}
CustomBiConsumerFunction2
class CustomBiConsumerFunction2 implements BiConsumer<TCSEmployee, Integer> {
private int bonus;
public CustomBiConsumerFunction2(int bonus) {
super();
this.bonus = bonus;
}
@Override
public void accept(TCSEmployee tcsEmployee, Integer yearOfPromotion) {
if(tcsEmployee.getYearOfJoining() >= yearOfPromotion) {
tcsEmployee.setSalary(tcsEmployee.getSalary() + bonus);
}
}
}
Below is the main class
Main Class
1 import java.util.ArrayList;
2 import java.util.List;
3 import java.util.function.BiConsumer;
4
5 public class BiConsumerDemo2 {
6 public static void main(String[] args) {
7 List<TCSEmployee> tcsEmployees = new ArrayList<TCSEmployee>();
8 tcsEmployees.add(new TCSEmployee("name1", false, 2019, 1000));
9 tcsEmployees.add(new TCSEmployee("name2", false, 2018, 1000));
10 tcsEmployees.add(new TCSEmployee("name3", false, 2017, 1000));
11 tcsEmployees.add(new TCSEmployee("name4", false, 2016, 1000));
12 tcsEmployees.add(new TCSEmployee("name5", false, 2015, 1000));
13 tcsEmployees.add(new TCSEmployee("name6", false, 2014, 1000));
14 tcsEmployees.add(new TCSEmployee("name7", false, 2013, 1000));
15
16 CustomBiConsumerFunction1 customBiConsumerFunction1 = new CustomBiConsumerFunction1();
17 CustomBiConsumerFunction2 customBiConsumerFunction2 = new CustomBiConsumerFunction2(5000);
18
19 BiConsumer<TCSEmployee, Integer> biConsumer = customBiConsumerFunction1.andThen(customBiConsumerFunction2);
20
21 for(TCSEmployee tcsEmployee : tcsEmployees) {
22 biConsumer.accept(tcsEmployee, 2017);
23 System.out.println(tcsEmployee);
24 }
25 }
26 }
In the above code, at line 16, I create an instance of CustomBiConsumerFunction1.
At line 17, I create an instance of CustomBiConsumerFunction2
At line 19, I chain both of them using BiConsumer’s default “andThen” method, which will return a new instance of BiConsumer, which is assigned to biConsumer variable.
When biConsumer variable’s accept method is called (refer to line 22), passing the TCSEmployee instance and yearOfPromotion, both BiConsumer interface implementations will get the same arguments. First customBiConsumerFunction1 is executed and then customBiConsumerFunction2 is executed.
Below is the output
Output
name1–>true–>2019–>6000
name2–>true–>2018–>6000
name3–>true–>2017–>6000
name4–>false–>2016–>1000
name5–>false–>2015–>1000
name6–>false–>2014–>1000
name7–>false–>2013–>1000