© Ralph Lecessi 2019
Ralph LecessiFunctional Interfaces in Javahttps://doi.org/10.1007/978-1-4842-4278-0_7

7. Suppliers

Ralph Lecessi1 
(1)
Kendall Park, NJ, USA
 

Section 7.1: The Supplier Interface

Supplier is a functional interface that is used to generate data. A Supplier object is specified with type parameter T. Its functional method, called get, takes no arguments and returns an object of type T.
@FunctionalInterface
public interface Supplier<T>
{
     T get();
}
A supplier that generates a random integer is defined as follows:
Supplier<Integer> generateInteger = () ->
{
    Random rand = new Random();
    return rand.nextInt(100);
};
A supplier that generates a string using a Scanner object is defined as follows:
Supplier<String> generateString = () ->
{

Get Functional Interfaces in Java: Fundamentals and Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.