Java BiFunction Example

BiFunction is a functional interface which represents a function that accepts two arguments and produces a result. This is the two-arity specialization of Function.

Java BiFunction Example

Create a Person class:
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
Here is an example of a BiFunction interface:

public class BiFunctionExample {

    public static void main(String[] args) {
  
       BiFunction<Person, Person, Integer> biFunction = (p1,p2) -> {
           return p1.getAge() + p2.getAge();
       };
  
       int totalAge = biFunction.apply(new Person("Ramesh", 10), 
                               new Person("ram", 10));
       System.out.println(totalAge);
  
    }
}

Reference



Comments