In this post, I will show how to use BiFunction functional interface with an example.
BiFunction functional interface is similar to Function functional interface. It takes an input and produces an output.
The difference lies in the number of inputs taken by the BiFunction functional interface.
Function functional interface takes one input and produced one output whereas BiFunction functional interface takes two inputs instead of one and produces one output.
BiFunction functional interface has abstract “apply” method which takes the two input and produces the output.
The class implementing the BiFunction functional interface has to provide the logic in the apply method.
Below is the compelete example
Main Class
1 package function;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.function.BiFunction;;
6
7 public class BiFunctionDemo1 {
8 public static void main(String[] args) {
9 List list = new ArrayList();
10 list.add(new BankEmployee1("name1", 10000, 1));
11 list.add(new BankEmployee1("name2", 20000, 2));
12 list.add(new BankEmployee1("name3", 30000, 3));
13 list.add(new BankEmployee1("name4", 40000, 4));
14 list.add(new BankEmployee1("name5", 50000, 5));
15
16 CustomBiFunction1 customBiFunction = new CustomBiFunction1();
17 for(BankEmployee1 bankEmployee : list) {
18 float result = customBiFunction.apply(bankEmployee.getSalary(), bankEmployee.getBonusPct());
19 bankEmployee.setBonus(result);
20 System.out.println(bankEmployee);
21 }
22 }
23 }
24
25 class CustomBiFunction1 implements BiFunction<Float, Float, Float> {
26 @Override
27 public Float apply(Float salary, Float percent) {
28 float result = salary + ((salary * percent)/100);
29 return result;
30 }
31 }
32
33 class BankEmployee1 {
34 private String name;
35 private float salary;
36 private float bonus;
37 private float bonusPct;
38
39 public BankEmployee1(String name, float salary, float bonusPct) {
40 super();
41 this.name = name;
42 this.salary = salary;
43 this.bonusPct = bonusPct;
44 }
45 public String getName() {
46 return name;
47 }
48 public void setName(String name) {
49 this.name = name;
50 }
51 public float getSalary() {
52 return salary;
53 }
54 public void setSalary(float salary) {
55 this.salary = salary;
56 }
57 public float getBonus() {
58 return bonus;
59 }
60 public void setBonus(float bonus) {
61 this.bonus = bonus;
62 }
63 public float getBonusPct() {
64 return bonusPct;
65 }
66 public void setBonusPct(float bonusPct) {
67 this.bonusPct = bonusPct;
68 }
69 @Override
70 public String toString() {
71 return name + " --> " + salary + " --> " + bonus;
72 }
73 }
The above code goes through a list of bank employees and apply bonus percent on their salary to get the actual bonus amount.
The bank employees are represented by class BankEmployee1.
The logic that calculates the bonus amount is represented by class CustomBiFunction1 as shown from line 25 to 31. The apply method of the implementation class take two inputs which in this case is employee salary and their allotted bonus percent. Using these two information, it produces and returns the output which is the bonus amount.
In the main method at line 9 to 14, we create a list of bank employees.
At line 16, we create an instance of CustomBiFunction1 named customBiFunction.
From line 17 to line 21, we loop through the list and for every employee we call apply method passing the salary and bonus percent information. In return we get the
bonus amount. Here the two inputs are employee salary and bonus percent and the output was the actual bonus amount.
In this way we can use the BiFunction functional interface.