Java 8 Consumer example

In this post, we are going to see about java 8 Consumer interface.
Consumer is single argument functional interface like Predicate but it does not return any value. As Consumer is functional interface, so it  can be used as assignment target for lambda expressions.
It has a functional method called accept(Object).
Lets use Consumer interface to print String:
package org.arpit.java2blog;
import java.util.function.Consumer;

public class Java8ConsumerExample {

 public static void main(String[] args) {
  
  Consumer<String> consumerString=s->System.out.println(s);
  consumerString.accept("Arpit");
 }
}

It is simple use of Consumer interface to print String. When you run above program, you will get below output:
Arpit
Example: Lets say you have student class as below:
package org.arpit.java2blog;

public class Student {

 private int id;
 private String name;
 private String gender;
 private int age;
 
 public Student(int id, String name, String gender, int age) {
  super();
  this.id = id;
  this.name = name;
  this.gender = gender;
  this.age = age;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 @Override
 public String toString() {
  return "Student [id=" + id + ", name=" + name + ", gender=" + gender
    + ", age=" + age + "]";
 }  
}
Now lets create Consumer object in main class and pass it to forEach method of list:
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Java8ConsumerExample {

 public static void main(String[] args) {
  
  List<Student> studentList=createStudentList(); 
  // Creating Consumer for student object which will be used in forEach method of list
  Consumer<Student> consumerForStudent= s->System.out.println("name : "+s.getName()+" == Age : "+s.getAge());
  studentList.forEach(consumerForStudent);
 }
 public static List<Student> createStudentList()
 {
  List<Student> studentList=new ArrayList<Student>();
  Student s1=new Student(1, "Arpit", "M", 19);
  Student s2=new Student(2, "John", "M", 17);
  Student s3=new Student(3, "Mary", "F", 14);
  Student s4=new Student(4, "Martin", "M", 21);
  Student s5=new Student(5, "Monica", "F", 16);
  Student s6=new Student(6, "Ally", "F", 20);
  
  studentList.add(s1);
  studentList.add(s2);
  studentList.add(s3);
  studentList.add(s4);
  studentList.add(s5);
  studentList.add(s6);
  return studentList;
 }
}
When you run above program, you will get below output:
name : Arpit == Age : 19
name : John == Age : 17
name : Mary == Age : 14
name : Martin == Age : 21
name : Monica == Age : 16
name : Ally == Age : 20

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012