An easy way to understand Supplier Interface in Java8.
Purpose:Represents a function that produces a result of a given type.
It acts as a “supplier” of values, generating them on demand without any input arguments.
Package:java.util.functionFunctional Interface: Contains only one abstract method(In fact just one method ), making it suitable for use with lambda expressions and method references.
It does not take any input arguments and returns the type of given Object.
Abstract Method:
T get(): Returns a result of type T.
Key Points:
Common Use Cases:
Generating values for testing or initial data setup.
Deferring execution of code until a value is needed.
Creating data on demand in lazy evaluation scenarios.
Handling optional values gracefully.
Asynchronous computations with CompletableFuture.
Combine with Other Functional Interfaces:
Can be used with other functional interfaces like Consumer to create concise data processing pipelines.
The methods inside of the Supplier interface
Its return type should be the type of the supplier
Returns a value of any type, specified by the generic type parameter T.
package java.util.function;
/**
* Represents a supplier of results.
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Before using Lambdas and streams, let’s look at the traditional way .
import java.util.function.Supplier;
// We can give Type like String , Integer etc
public class SupplierDemo implements Supplier<String> {
@Override
public String get() {
return "Hello from get method";
}
public static void main(String[] args) {
Supplier<String> supplier = new SupplierDemo();
System.out.println(supplier.get());
}
}
“For the type parameter T in Supplier<T>, you can use not only specific classes like String or Integer but also any valid Java reference type, including custom classes, interfaces, and wrapper classes for primitive types. However, primitive types themselves cannot be used directly as type parameters; instead, you should use their corresponding wrapper classes.”
Example can’t use int , so use Integer
public class SupplierDemo implements Supplier<Integer> {
@Override
public Integer get() {
return 42; // or any other integer value
}
public static void main(String[] args) {
SupplierDemo supplierDemo = new SupplierDemo();
int result = supplierDemo.get();
System.out.println("The value is: " + result);
}
}
Now , lets convert it to a lambda expression.
package steams.examples;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<String> supplier = () -> "Hello from get method";
// Now you can use the supplier to get the value
String result = supplier.get();
System.out.println(result);
}
}
I used with Stream
package steams.examples;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<String> supplier = () -> "Hello no suppliers";
// Now you can use the supplier to get the value
String result = supplier.get();
System.out.println("calling Get "+result);
List<String> names = Arrays.asList("Ana","Barbie");
// since there is value in the names Ana will be printed
System.out.println(names.stream().findAny().orElseGet(supplier));
List<String> futureNames = Arrays.asList();
// since no value in the list , supplier will be called
System.out.println(futureNames.stream().findAny().orElseGet(supplier));
}
}
Output-
calling Get Hello no suppliers
Ana
Hello no suppliers
Further reference-https://www.youtube.com/watch?v=Tapz6_T5oHY&t=676s