Java 8 Supplier Interface

11 months ago codedestine 0

In this tutorial, we will learn about Java 8 Supplier interface.

Java 8 Supplier Interface

Supplier Interface :-

Supplier ( java.util.function.Supplier ) is a functional interface that has one abstract method declared in it. It represents an operation by which you can generate new values in the stream.

Abstract method :- 

T  get (  ) :- The abstract method get (  )  is a functional method. It does not accept any argument but return newly generated values ( T ) in the stream. 

Reference :-  Supplier Interface JavaDocs 

Java 8 Stream API have two methods where Supplier interface are used as an argument, they are as follows :-

  1. public static <T> Stream <T> generate ( Supplier <T> s )
  2. <R> R collect ( Supplier <R> supplier, BiConsumer <R, ? super T> accumulator, BiConsumer <R, R> combiner )

There are four specialised ( primitive specific ) form of Java 8 Supplier interface, which can be used in case of generating int, longdouble and boolean. They are as follows :-

  1. IntSupplier interface ( JavaDocs ) :- It represents an operation by which you can generate new int values in the stream.
  2. LongSupplier interface ( JavaDocs ) :- It represents an operation by which you can generate new long values in the stream.
  3. DoubleSupplier interface ( JavaDocs ) :- It represents an operation by which you can generate new double values in the stream.
  4. BooleanSupplier interface ( JavaDocs ) :- It represents an operation by which you can generate new boolean values in the stream.

Employee ( POJO ) :-  Used in our example.


  public class Employee {

    private long empId;
    private String name;
    private int age;
    private String designation;

    public Employee(long empId, String name, int age, String designation) {
      super();
      this.empId = empId;
      this.name = name;
      this.age = age;
      this.designation = designation;
    }

    public long getEmpId() {
      return empId;
    }
    public void setEmpId(long empId) {
      this.empId = empId;
    }
    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;
    }
    public String getDesignation() {
      return designation;
    }
    public void setDesignation(String designation) {
      this.designation = designation;
    }

 }

Example – 1 :- 

This example will show you, how to create and call different methods of a supplier interface.

  public static void main(String[] args){
 
    Supplier<Employee> supplier = ( ) -> { return new Employee(1,"Robert",35,"Manager"); };
 
    Employee emp = supplier.get();
    System.out.println(emp.getDesignation()); 
  } 

Result :- 

Manager 

Example – 2 :- 

In this example, we will generate new stream by using stream’s generate() static method. This method takes one supplier as an argument.

  public static void main(String[] args){
 
    Supplier<Employee> supplier = ( ) -> { return new Employee(1,"Robert",35,"Manager"); };
 
    Stream<Employee> s = Stream.generate(supplier).limit(1);
    List<Employee> empList = s.collect(Collectors.toList());
    empList.forEach( e -> System.out.println(e.getAge())); 
  }   

Result :- 

35 

That’s all for Java 8 Supplier Interface. If you liked it, please share your thoughts in comments section and share it with others too.