The Supplier
functional interface in Java is part of the java.util.function
package and is used when we need to supply or produce a result without taking any input. It is a functional interface, meaning it can be represented as a lambda expression or method reference.
The Supplier
interface has a single abstract method:
T get();
This method returns an object of type T
and takes no arguments.
Usage of Supplier
- Lambda Expression: We can use a lambda expression to define the logic for producing a result.
- Method Reference: If we have an existing static or instance method that matches the
Supplier
signature (no parameters, return a value), we can use a method reference.
Here are a few examples to demonstrate how to use Supplier
:
Example 1: Basic Supplier Usage
package org.kodejava.util.function;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
// Using a Supplier to generate a string
Supplier<String> stringSupplier = () -> "Hello, Supplier!";
System.out.println(stringSupplier.get());
// Output: Hello, Supplier!
}
}
Example 2: Supplier with Random Number
package org.kodejava.util.function;
import java.util.function.Supplier;
import java.util.Random;
public class RandomNumberExample {
public static void main(String[] args) {
// Using a Supplier to provide a random int
// Random number between 0-99
Supplier<Integer> randomSupplier = () -> new Random().nextInt(100);
System.out.println("Random number: " + randomSupplier.get());
System.out.println("Another random number: " + randomSupplier.get());
}
}
Example 3: Method Reference with Supplier
package org.kodejava.util.function;
import java.util.function.Supplier;
public class SupplierMethodReferenceExample {
public static void main(String[] args) {
// Supplier using method reference
Supplier<Double> piSupplier = Math::random;
System.out.println("Random value using method reference: " + piSupplier.get());
}
}
Example 4: Supplying an Object
package org.kodejava.util.function;
import java.util.function.Supplier;
class Person {
String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{name='" + name + "'}";
}
}
public class ObjectSupplierExample {
public static void main(String[] args) {
// Using a Supplier to construct an object
Supplier<Person> personSupplier = () -> new Person("Rosa");
Person person = personSupplier.get();
System.out.println(person);
// Output: Person{name='Rosa'}
}
}
Where to Use Supplier
- Lazy Evaluation: To delay the execution of some logic until a value is needed.
- Factories: To create new objects.
- Caching or Computed Values: Use
Supplier
to generate values that are expensive to compute only when needed.
By using Supplier
, we can encapsulate the logic of generating or supplying values while following the functional programming paradigm in Java.
Latest posts by Wayan (see all)
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025