Java Consumer Example

Consumer is a functional interface and that represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

Java Consumer Example

Let's first 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 Consumer interface:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ConsumersExample {

 public static void main(String[] args) {

  List<Person> listOfPerson = new ArrayList<Person>();
  listOfPerson.add(new Person("abc", 27));
  listOfPerson.add(new Person("mno", 26));
  listOfPerson.add(new Person("pqr", 28));
  listOfPerson.add(new Person("xyz", 27));

  listOfPerson.forEach((person) -> {
   System.out.println(" Person name : " + person.getName());
   System.out.println(" Person age : " + person.getAge());
  });
  
  
  // Second example
  Consumer<Person> consumer = (person) -> {
   System.out.println(person.getName());
   System.out.println(person.getAge());
  };
  consumer.accept(new Person("Ramesh", 30));
 }
}
Output:
 Person name : abc
 Person age : 27
 Person name : mno
 Person age : 26
 Person name : pqr
 Person age : 28
 Person name : xyz
 Person age : 27
Ramesh
30





Comments