October 31, 2022

Supplier Functional Interface Java Examples

In this post we’ll see examples of java.util.function.Supplier functional interface.

Supplier functional interface represents an operation that accepts no argument and supplies a result. In Supplier interface there is one abstract method get() and there is no default or static interface method.

If you are writing a Lambda expression that needs a value returned then that lambda expression can be written as an implementation of Supplier built-in functional interface.

Supplier functional interface examples

1. If you want some random number to be generated, that can be implemented as get() method of Supplier interface.

import java.util.function.Supplier;

public class SupplierExample {
  public static void main(String[] args) {
    Supplier<Double> randomNoGen = () -> Math.random();
    System.out.println(randomNoGen.get());
    System.out.println(randomNoGen.get());
  }
}
Output
0.9507895772946557
0.11609076109430083

In the program statement; Supplier randomNoGen = () -> Math.random(); is the implementation of Supplier as a lambda expression. Since this implementation is an instance of a functional interface so assigned to variable of type Supplier.

When randomNoGen.get() method is called Java can infer from the context, due to “target typing”, where to look for the implementation of get() method.

2. If you want current date at several places in your application, you can implement it as a Supplier.

import java.time.LocalDate;
import java.util.function.Supplier;

public class SupplierExample {
  public static void main(String[] args) {
    Supplier<LocalDate> currDate = () -> LocalDate.now();
    System.out.println(currDate.get());
  }
}

3. Using it as a supplier of class object. If there is a class Employee then you can create objects of this class using Supplier interface.

public class Employee {
  private String name;
  private String dept;
  private int salary;
  Employee(){};
  Employee(String name, String dept, int salary){
    this.name = name;
    this.dept = dept;
    this.salary = salary;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int salary) {
    this.salary = salary;
  }
  public String getDept() {
    return dept;
  }
  public void setDept(String dept) {
    this.dept = dept;
  }
  @Override
  public String toString() {
    // TODO Auto-generated method stub
    return getName() + " " + getDept() + " " + getSalary();
  }
}
public class SupplierExample {
  public static void main(String[] args) {	
    // Invokes no-arg constructor
    Employee emp = getEmpObject(Employee::new);
    System.out.println(emp);
    // Invokes constructor with parameters
    emp = getEmpObject(() -> new Employee("David", "IT", 12000));
    System.out.println(emp);
  }

  public static Employee getEmpObject(Supplier<Employee> sup) {
    return sup.get();
  }
}

Supplier functional interface in JDK

Built-in functional interfaces are used extensively with in the JDK itself. In the Java Stream API there is a method generate() that takes Supplier as an argument.

generate(Supplier<T> s)- Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.

We can use this method to generate a stream of random numbers.

public class SupplierExample {
  public static void main(String[] args) {	
    Stream.generate(()->Math.random()).limit(3).forEach(System.out::println);
  }
}
Output
0.17411307331904347
0.9293020926865666
0.9854950033297908

That's all for the topic Supplier Functional Interface Java Example. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment