Java

How Does the Supplier Functional Interface Work in Java

In Java, the “Supplier” functional interface is assistive when there is a requirement to return an object of type “T”. This interface does not accept any object as input but generates a value of type “T”. Also, this interface comes into effect while dealing with the “lambda expressions”. Summing it up, it can be utilized any time a function needs to generate an outcome without any data given to it.

This blog will demonstrate the significance and working of the “Supplier” functional interface in Java.

How Does the Supplier Functional Interface Work in Java?

Supplier” refers to a functional interface that generates the results without accepting any inputs. The results produced each time can be identical or different. The interface comprises the method i.e., “get()” that returns a value of type “T”. It corresponds to the functional method of the interface that gives a result each time it’s invoked.

Syntax

T get()

In this syntax, “T” signifies the type of the result.

Before moving on to the examples, import the below-given package to work with the “Supplier” functional interface:

import java.util.function.Supplier;

Example 1: Applying the “Supplier Functional Interface” to Return a Randomly Generated Double Value
In this example, the “Supplier” functional interface can be implemented to fetch a randomly generated double value:

public class Supplierinterface {
 public static void main(String args[]){
  Supplier value = () -> Math.random();
  System.out.println(value.get());
}}

In the above code lines:

  • Apply a “Supplier” interface having the specified type i.e., “double” to generate the random double value.
  • After that, apply the “get()” method to return the randomly generated double value.

Output

In the above-generated outcome, it can be observed that the random double values are being generated accordingly.

Include the below-stated additional package in the next example to invoke all the classes within the “java.util” package:

import java.util.*;

Example 2: Applying the “Supplier Functional Interface” to Return the Specified Randomly Generated Integer Values
This example implements the “Supplier” interface to generate the specified number of random integer values:

public class Supplierinterface {
 public static void main(String args[]){
  Supplier value = () -> new Random().nextInt();
  for (int counter = 0;counter<=3; counter++)
   System.out.println(value.get());
}}

According to this code block, perform the below-stated steps:

  • Likewise, apply the “Supplier” interface with the type specified as “Integer”.
  • Also, create an instance of the “Random” class and apply the “nextInt()” method to generate the random integer values.
  • Now, apply the “for” loop to specify the count of the randomly generated integer values i.e., “0-3 -> 4” in this case.
  • It is such that “4” random integer values will be returned via the “get()” method invoked four times in accordance with the loop.

Output

Here, it can be implied that the specified number of random integers are returned/generated appropriately.

Conclusion

Supplier” corresponds to a functional interface in Java that generates the results without accepting any inputs with the help of its method i.e., “get()”. This blog demonstrated the working of the “Supplier” functional interface in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.