The Java Supplier interface of the java.util.function package is a functional interface that is often used as an assignment target for lambda expressions. The get() method is the class’s only abstract method, which takes no input and returns an output.

For more information on functional interfaces in Java click here: What is a Java Functional Interface?.
For more information on lambdas in Java click here: What are Lambdas in Java?

Supplier Interface Source Code

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

Supplier Interface Example

Below is an example of creating the SupplierExample class by implementing Supplier. This get() method takes no input and returns a random Integer.

import java.util.function.Supplier;
import java.util.Random;

public class SupplierExample implements Supplier<Integer> {
    private Random random = new Random();

    public Integer get() {
        return random.nextInt();
    }

}

Creating a Supplier Instance with Lambda Expression

Below is an example of using a lambda expression to create an instance of Supplier. This get() method takes no input and returns a random Integer.

Random random = new Random();
Supplier supplier = () -> random.nextInt();

Leave a Reply

What is the Java Supplier Interface?