BiFunction’s andThen Example

In this post under Java, we will explain with an example the andThen method under BiFunction functional interface.

BiFunction’s andThen method is used to chain a BiFunction interface implementation with one or more Function interface implementations.

Lets consider an example. I have a list of employess with their salary and percent of bonus to apply information. Once bonus is calculated using employee’s salary and percent of bonus information, we will apply Country tax and State tax to get the final bonus.

Lets first create a Employee class which will hold the salary and percent of bonus information.


class BankEmployee2 {
    private String name;
    private float salary;
    private float bonus;
    private float bonusPct;

    public BankEmployee2(String name, float salary, float bonusPct) {
        super();
        this.name = name;
        this.salary = salary;
        this.bonusPct = bonusPct;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
    public float getBonus() {
        return bonus;
    }
    public void setBonus(float bonus) {
        this.bonus = bonus;
    }
    public float getBonusPct() {
        return bonusPct;
    }
    public void setBonusPct(float bonusPct) {
        this.bonusPct = bonusPct;
    }
    @Override
    public String toString() {
        return name + " --> " + salary + " --> " + bonus;
    }
}

Now lets create BiFunction functional interface implementation that takes employee’s salary and percent of bonus to calculate gross bonus.

BonusFunction


class BonusFunction implements BiFunction<Float, Float, Float> {
    @Override
    public Float apply(Float salary, Float percent) {
        float result = salary + ((salary * percent)/100);
        return result;
    }
}

Next we will create two Function functional interface implementations, one to apply Country tax and another to apply State tax.

CountryTaxFunction


class CountryTaxFunction implements Function<Float, Float> {
    private float tax;

    public CountryTaxFunction(float tax) {
        super();
        this.tax = tax;
    }

    @Override
    public Float apply(Float bonus) {
        float result = bonus - ((bonus * tax)/100);
        return result;
    }
}

StateTaxFunction


class StateTaxFunction implements Function<Float, Float> {
    private float tax;

    public StateTaxFunction(float tax) {
        super();
        this.tax = tax;
    }

    @Override
    public Float apply(Float bonus) {
        float result = bonus - ((bonus * tax)/100);
        return result;
    }
}

Now we will combine all three of them using BiFunction’s andThen method. Below is the main class for your reference

Main Class


1  public class BiFunctionDemo2 {
2      public static void main(String[] args) {
3          List<BankEmployee2> list = new ArrayList<BankEmployee2>();
4          list.add(new BankEmployee2("name1", 10000, 1));
5          list.add(new BankEmployee2("name2", 20000, 2));
6          list.add(new BankEmployee2("name3", 30000, 3));
7          list.add(new BankEmployee2("name4", 40000, 4));
8          list.add(new BankEmployee2("name5", 50000, 5));
9          
10         BonusFunction bonusFunction = new BonusFunction();
11         CountryTaxFunction countryTaxFunction = new CountryTaxFunction(5);
12         StateTaxFunction stateTaxFunction = new StateTaxFunction(5);
13         
14         BiFunction<Float, Float, Float> bonusAndTaxFunction = bonusFunction.andThen(countryTaxFunction).andThen(stateTaxFunction);
15         
16         for(BankEmployee2 bankEmployee : list) {
17             float result = bonusAndTaxFunction.apply(bankEmployee.getSalary(), bankEmployee.getBonusPct());
18             bankEmployee.setBonus(result);
19             System.out.println(bankEmployee);
20         }
21     }
22 }

As you can see in the above code, from line 3 to 8 we create a list of employees.

Then at line 10, 11, and 12 we create instances of BonusFunction named ‘bonusFunction’, CountryTaxFunction named ‘countryTaxFunction’, and StateTaxFunction
named ‘stateTaxFunction’.

At line 14, we combine all three using andThen and assign it to variable named ‘bonusAndTaxFunction’. Below is the code snippet


BiFunction<Float, Float, Float> bonusAndTaxFunction = bonusFunction.andThen(countryTaxFunction).andThen(stateTaxFunction);

The order in which these functions are executed is from left to right. First the bonusFunction is executed and then countryTaxFunction and then stateTaxFunction.

The return of each andThen method is a new BiFunction interface implementation, so it is easy to chain multiple Function Functional Interface.

This is how we use BiFunction functional interface’s andThen method.

Leave a Reply