What is Supplier Interface in Java 8

Just supply our required objects and it won’t take any input then we will use the Supplier Predefined Functional Interface. It is introduced in Java 8. It is available in the java.util.function package. Suppose we require the implementation of a Lambda Expression with the predefined functional interface and supply our object and don’t want to need to provide any input and return output. In that case, we prefer the Supplier Predefined Functional Interface. It is having only one method i.e. get() and there is no other method in it. It is enabling functional programming.
Prototype:

public interface Supplier<T>
{
public T get();
}

Type Parameters:
T – It is a type of result supplied by this supplier.
Example:
In this example, we will supply the Date object to the Supplier functional Interface and print the output of the current date.

import java.util.Date;

import java.util.function.Supplier;

public class Test {

public static void main(String[] args) {

 Supplier<Date> s= ()->new Date();

 System.out.println("Supplier(Date):"+s.get());
}
}

Output: 

Supplier(Date):Fri Sep 09 11:47:58 IST 2022

Example:
In this example, we will supply the Student object to the Supplier Functional Interface and print the output of the student detail.

import java.util.function.Supplier;

class Student {

private int id;

private String name;

private String gender;

private String course;

public Student() {

}

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 String getCourse() {

return course;

}

public void setCourse(String course) {

this.course = course;
}
}

public class Test {

public static void main(String[] args) {

Supplier<Student> s = () -> {

Student stuDetail = new Student();

stuDetail.setId(1);

stuDetail.setName("Raj");

stuDetail.setGender("Male");

stuDetail.setCourse("BCA");

return stuDetail;

};

System.out.println("Student Detail: " + "Id:" + s.get().getId() + " Name:" + s.get().getName() + " Gender:"

+ s.get().getGender() + " Course:" + s.get().getCourse());
    }
}

Output: 

Student Detail: Id: 1 Name: Raj Gender: Male Course: BCA

Example:
In this example, we will supply the List object to the Supplier Functional Interface and print the output of the list of the students.

import java.util.ArrayList;

import java.util.List;

import java.util.function.Supplier;

class Student {

private int id;

private String name;

private String gender;

private String course;

public Student() {

}
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 String getCourse() {

return course;

}

public void setCourse(String course) {

this.course = course;

}
}

public class Test {

public static void main(String[] args) {

Supplier<List<Student>> s = () -> {

List<Student> listStdnt = new ArrayList<Student>();

Student stuDetail1 = new Student();

stuDetail1.setId(1);

stuDetail1.setName("A");

stuDetail1.setGender("Male");

stuDetail1.setCourse("BCA");

Student stuDetail2 = new Student();

stuDetail2.setId(2);

stuDetail2.setName("B");

stuDetail2.setGender("Male");

stuDetail2.setCourse("BCA");

Student stuDetail3 = new Student();

stuDetail3.setId(3);

stuDetail3.setName("C");

stuDetail3.setGender("Female");

stuDetail3.setCourse("BCA");

Student stuDetail4 = new Student();

stuDetail4.setId(4);

stuDetail4.setName("D");

stuDetail4.setGender("Female");

stuDetail4.setCourse("BCA");

Student stuDetail5 = new Student();

stuDetail5.setId(5);

stuDetail5.setName("E");

stuDetail5.setGender("Male");

stuDetail5.setCourse("BCA");

listStdnt.add(stuDetail1);

listStdnt.add(stuDetail2);

listStdnt.add(stuDetail3);

listStdnt.add(stuDetail4);

listStdnt.add(stuDetail5);

return listStdnt;

};

System.out.println("List of Students:");

for (Student stuDetail : s.get()) {

System.out.println("Id: " + stuDetail.getId() + " Name: " + stuDetail.getName() + " Gender: "

+ stuDetail.getGender() + " Course: " + stuDetail.getCourse());

     }
  }
}

Output: 

List of Students:
Id: 1 Name: A Gender: Male Course: BCA
Id: 2 Name: B Gender: Male Course: BCA
Id: 3 Name: C Gender: Female Course: BCA
Id: 4 Name: D Gender: Female Course: BCA
Id: 5 Name: E Gender: Male Course: BCA

Conclusion:
This topic is explained What is a Supplier and how to use it? How to use functional programming with the help of the Supplier? 

Leave a Comment