Java 8 java.util.function.Supplier Tutorial with Examples

Introduction: Tutorial explains the in-built functional interface Supplier<T> introduced in Java 8. It explains with the help of examples how the Supplier interface is to be used via its get() method.

What is java.util.function.Supplier: Supplier<T> is an in-built functional interfaceClick to Read tutorial on Functional Interfaces introduced in Java 8 in the java.util.function package. Supplier can be used in all contexts where there is no input but an output is expected.

Since Supplier is a functional interface, hence it can be used as the assignment target for a lambda expressionClick to read Lambda Expressions tutorial or a method referenceClick to read tutorial on Method References.

Function Descriptor of Supplier<T>: Supplier’s Function Descriptor is () -> T . This means that there is no input in the lambda definition and the return output is an object of type T. To understand Function Descriptors in details you can refer the function descriptor tutorialTutorial explaining function descriptors.

Advantage of predefined java.util.function.Supplier: In all scenarios where there is no input to an operation and it is expected to return an output the in-built functional interface Supplier<T> can be used without the need to define a new functional interface every time.

java.util.function.Supplier source code

java.util.function.Supplier source code
@FunctionalInterface
public interface Supplier<T> {
    /**
     * Gets a result.
     * @return a result
     */
    T get();
}
Salient Points regarding Supplier<T>’s source code :

  • Supplier has been defined with the generic type T which is the same type which its get() methods return as output.
  • get() method is the primary abstract method of the Supplier functional interface. Its function descriptor being () -> T . I.e. get() method takes no input and returns an output of type T. I will explain usage of get() with detailed example in the next section.
  • All lambda definitions for Supplier must be written in accordance with get() method’s signature, and conversely all lambdas with the same signature as that of get() are candidates for assignment to an instance of Supplier interface.

Usage of get() method of Supplier:
To understand the get() method lets take a look at the SupplierFunctionExample’s code below, post which I have explained in detail how the code works-

Code showing usage of Supplier.get() method
//SupplierFunctionExample.java
import java.util.Date;
import java.util.function.Supplier;
public class SupplierFunctionExample {
 public static void main(String args[]) {
  //Supplier instance with lambda expression
  Supplier<String> helloStrSupplier = () -> new String("Hello");
  String helloStr = helloStrSupplier.get();
  System.out.println("String in helloStr is->"+helloStr+"<-");
  
  //Supplier instance using method reference to default constructor
  Supplier<String> emptyStrSupplier = String::new;
  String emptyStr = emptyStrSupplier.get();
  System.out.println("String in emptyStr is->"+emptyStr+"<-");
  
  //Supplier instance using method reference to a static method
  Supplier<Date> dateSupplier= SupplierFunctionExample::getSystemDate;
  Date systemDate = dateSupplier.get();
  System.out.println("systemDate->" + systemDate);
 }
 public static Date getSystemDate() {
  return new Date();
 }
}
 OUTPUT of the above code
String in helloStr is->Hello<-
String in emptyStr is-><-
systemDate->Wed Dec 16 19:18:15 IST 2015
Explanation of above example’s Code & Output

  • SupplierFunctionExample is my class with 2 methods – main() & getSystemDate().
  • getSystemDate() is a static method which simply returns the current system date and does not take any input. The method signature matches the function descriptor of Supplier i.e. () -> T .
  • In main() method I have shown how to instantiate a Supplier interface instance in following 3 ways
    1. Using a Lambda Expression: I have defined a a lambda expression which takes no input and returns a new String object with value set to “hello”. This lambda I have assigned to a Supplier<String> instance named helloStrSupplier. Invoking functional method get() on helloStrSupplier gives us a String helloStr which is then printed to show that it indeed contains the value “hello”.
    2. Using a Method Reference to default constructor of String: Method Reference to the default constructor of String is used to create a Supplier<String> instance named emptyStrSupplier. emptyStrSupplier is then used to create a String object named emptyStr using the get() method. emptyStr‘s value is then printed to show its value is empty as defined.
    3. Using a Method Reference to getSystemDate(): Method Reference to the getSystemDate() method of SupplierFunctionExample class is used to create a Supplier<Date> instance named dateSupplier. dateStrSupplier is then used to create a Date object named systemDate by invoking get() method on it. systemDate‘s value is then printed to show its value. Value printed is of 16-Dec when I ran this example.

Summary: In this tutorial we looked at what is the Supplier<T> in-built interface defined in Java 8 and what is its main advantage. We then looked at how to use the Supplier<T> interface using its get() method with an example.

 

Digiprove sealCopyright © 2014-2017 JavaBrahman.com, all rights reserved.