The Supplier Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which does not take in any argument but produces a value of type T.
Hence this functional interface takes in only one generic namely:-
- T: denotes the type of the result
The lambda expression assigned to an object of Supplier type is used to define its get() which eventually produces a value. Suppliers are useful when we don’t need to supply any value and obtain a result at the same time.
The Supplier interface consists of only one function:
1. get()
This method does not take in any argument but produces a value of type T.
Syntax:
T get()
Returns: This method returns a value of type T.
Below is the code to illustrate get() method:
Program:
import java.util.function.Supplier; public class Main { public static void main(String args[]) { // This function returns a random value. Supplier<Double> randomValue = () -> Math.random(); // Print the random value using get() System.out.println(randomValue.get()); } } |
0.5685808855697841
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- OptionalLong orElseThrow(Supplier) method in Java with examples
- OptionalDouble orElseThrow(Supplier) method in Java with examples
- OptionalInt orElseThrow(Supplier) method in Java with examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Comparator Interface in Java with Examples
- List Interface in Java with Examples
- SortedSet Interface in Java with Examples
- SortedMap Interface in Java with Examples
- Function Interface in Java with Examples
- ToLongFunction Interface in Java with Examples
- LongToIntFunction Interface in Java with Examples
- ToIntFunction Interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

