Supplier Interface In Java

The Supplier functional interface is also a type of functional interface that does not take any input or argument and returns a single output. The Supplier interface takes only one generic type, the type of data it is going to return. It is having only one method as get() and there is no other method. The get() is the abstract method of the Supplier.

If we want to implement of a lambda expression with the predefined functional interface and supply our object and don’t want to to provide input. So, we prefer the Supplier Predefined Functional Interface.

  1. The Supplier functional interface is also a type of functional interface that does not take any input or argument and returns a single output.
  2. It is having only one method as get() and there is no other method.
  3. The get() method is the abstract method of the Supplier.
  4. It is enabling functional programming.

Example of Supplier Interface

public static void main(String[] args) { 
    Supplier<String> supplier = 
      () -> "Hello World";

    System.out.println(supplier.get());        
} 

Output:
Hello World

Example of Supplier Interface

public static void main(String[] args) { 
    Supplier<Integer> supplier = 
      () -> 500;

    System.out.println(supplier.get());        
} 

Output:
500

Example of Supplier Interface

public static void main(String[] args) { 
    Supplier<Integer> supplier = 
      () -> 100;

    System.out.println(supplier.get());        
} 

Output:
100

Example of Supplier Interface

public static void main(String[] args) { 
    Supplier<Date>  dateSupplier = 
        ()->new Date();;

    System.out.println(dateSupplier.get());     
    
    
} 

Output:
Sun Mar 31 10:37:48 IST 2024