What is a Supplier Interface in Java?
This lesson talks about the second functional interface, which is the Supplier.
Table of Contents
What is the Supplier interface?
It is the opposite of the Consumer interface. It takes an input and returns a value, so it supplies a value.
The Supplier
interface is a functional interface defined in the java.util.function
package. It represents a supplier of values, meaning it does not take any arguments but produces a result of a specified type.
The Supplier
interface is often used in scenarios where you need to generate or provide values on demand.
The Supplier
interface declares a single abstract method called get()
, which has no arguments and returns a value of the specified type. Here's the method signature of the get()
method:
Syntax:
T get();
The type parameter T
represents the type of the value that the Supplier
produces. It can be any valid Java type, including primitive types and objects.
To use the Supplier
interface, you can create an instance of it using a lambda expression or method reference that implements the get()
method.
Examples
Generate random number
Here's an example that demonstrates how to use a Supplier
to generate random integers:
import java.util.Random;
import java.util.function.Supplier;
public class SupplierInterface {
public static void main(String[] args) {
Supplier<Integer> randomNumberSupplier =
() -> {
Random random = new Random();
return random.nextInt(100);
};
// Calling the get() method to get a random number
int randomNumber = randomNumberSupplier.get();
System.out.println("Random number: " + randomNumber);
}
}
In the example, we create a Supplier<Integer>
using a lambda expression that generates a random integer between 0
and 99
(inclusive) using the Random
class. Then, we call the get()
method on the Supplier
instance to obtain a random number.
The Supplier
interface is commonly used in functional programming, particularly in situations where lazy evaluation or deferred execution of a value is required. It provides a simple and flexible way to generate values on demand without the need for explicit method arguments.
Note that the Supplier
interface is part of the Java 8 functional interfaces introduced to support lambda expressions and functional programming constructs. It is widely used in conjunction with other functional interfaces like Consumer
, Predicate
, and Function
to build powerful functional pipelines and enable functional programming patterns in Java.
String operations
Let us see an example where we give input and the supplier returns the value using its get
method.
A simple code snippet explaining how we input a string, and how the Supplier’s get()
method returns the very string.
import java.util.function.Supplier;
public class SupplierStringOperations {
public static void main(String[] args) {
Supplier<String> s = () -> "Hello, Developers!";
System.out.println(s.get()); // Hello, Developers!
}
}
Finally, another example is where we input something and do some checks, math, and string manipulations
import java.util.function.Supplier;
public class SupplierStringManipulations {
public static void main(String[] args) {
String fruit = "Apple";
Supplier<Boolean> booleanSupplier = () -> fruit.length() == 5;
Supplier<Integer> integerSupplier = () -> fruit.length() * 5;
Supplier<String> stringSupplier = () -> fruit.toLowerCase();
System.out.println(booleanSupplier.get());
System.out.println(integerSupplier.get());
System.out.println(stringSupplier.get());
}
}
The above example prints the following on the console:
true
25
apple
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.