Supplier Interface in Java with Examples
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:
This method does not take in any argument but produces a value of type T.
Returns: This method returns a value of type T.
Below is the code to illustrate get() method:
Supplier<Double> randomValue = () -> Math.random();
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!