Java BiConsumer Example

BiConsumer is a functional interface which represents an operation that accepts two input arguments and returns no result. This is the two-arity specialization of Consumer. Unlike most other functional interfaces, BiConsumer is expected to operate via side-effects.

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 BiConsumer interface:
public class BiConsumersExample {

    public static void main(String[] args) {
  
        BiConsumer<Person, Person> biConsumer = (p1, p2) -> {
             System.out.println(" print first person");
             System.out.println(p1.getName());
             System.out.println(" print second person");
             System.out.println(p2.getName());
       };
  
       biConsumer.accept(new Person("Ramesh", 10), new Person("ram", 10));
    }
}
Output:
 print first person
Ramesh
 print second person
ram

Reference


Comments