Java 8 Supplier example

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

public class Java8SupplierExample {

 public static void main(String[] args) {
  
  Supplier<String> supplier=()->"Arpit";
  System.out.println(supplier.get());
 }
}
It is simple use of supplier interface to get String object. When you run above program, you will get below output:
Arpit

Passing Supplier to Stream API methods:

Supplier can be passed as argument to Stream 's method. In this example, we will pass Supplier to  map method of Stream API.
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.stream.Stream;

public class Java8SupplierExample {

	 public static void main(String[] args) {
		  
		  List<Student> studentList=createStudentList();   
		  Stream<String> names = studentList.stream().map(Student::getName);
	        names.forEach(n -> System.out.println(n));
		 }
 
 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:
Arpit
John
Mary
Martin
Monica
Ally

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