Supplier is a predefined functional interface in Java used to supply or generate data without taking any input.
In simple words:
Supplier provides data or objects whenever requested.
Main Package
java.util.function
Supplier Introduced In
Supplier functional interface was introduced in:
Java 8
Why Supplier was Introduced?
Before Java 8:
- Object creation logic was repetitive
- Lazy object generation was difficult
- Functional-style data generation was missing
- Reusable supplier logic was limited
Supplier Goal
No Input
|
v
Supplier Executes
|
v
Data/Object Generated
Supplier Declaration
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Important Point
Supplier contains:
One Abstract Method
called:
get()
Method Signature
T get()
How Supplier Works?
- Takes no input
- Generates value/object
- Returns result
Supplier Working Flow
Supplier Triggered
|
v
Generation Logic Executes
|
v
Value Returned
Basic Supplier Example
Supplier<String> supplier =
() -> "Hello Java";
System.out.println(
supplier.get()
);
Output
Hello Java
Another Example
Supplier<Integer> random =
() -> (int)(Math.random() * 100);
System.out.println(
random.get()
);
Possible Output
57
Supplier with Streams
Supplier is heavily used with:
Stream.generate()
Example
Stream.generate(
() -> "Java"
)
.limit(3)
.forEach(System.out::println);
Output
Java Java Java
Stream.generate() Flow
Supplier Invoked Repeatedly
|
v
New Values Generated
|
v
Infinite Stream Produced
Supplier for Object Creation
Supplier<Date> dateSupplier =
Date::new;
System.out.println(
dateSupplier.get()
);
Method Reference with Supplier
Supplier works well with method references.
Example
Supplier<List<String>> listSupplier =
ArrayList::new;
Supplier Flow with Method Reference
Supplier Triggered
|
v
Constructor Called
|
v
Object Created
Supplier for Lazy Initialization
Supplier is commonly used for lazy object creation.
Example
Supplier<Connection> connectionSupplier =
() -> createConnection();
Lazy Initialization Flow
Supplier Defined
|
v
No Object Created Yet
|
v
get() Called
|
v
Object Created On Demand
Supplier vs Consumer vs Predicate vs Function
| Interface | Input | Output | Main Purpose |
|---|---|---|---|
| Supplier | No Input | Returns Value | Generate Data |
| Consumer | Takes Input | No Return | Perform Action |
| Predicate | Takes Input | boolean | Condition Check |
| Function | Takes Input | Returns Value | Transformation |
Supplier in Banking Systems
Banking applications use Supplier for:
- Lazy database connections
- Transaction ID generation
- Session token generation
- Report generation
- On-demand analytics
Banking Flow
Supplier Triggered
|
v
Secure Transaction ID Generated
|
v
Payment Processing Starts
Supplier in E-Commerce Systems
E-commerce platforms use Supplier for:
- Order ID generation
- Coupon generation
- Inventory loading
- Recommendation fetching
- Lazy cache initialization
E-Commerce Flow
Customer Request
|
v
Supplier Generates Recommendations
|
v
Products Displayed
Supplier in Spring Boot
Spring Boot applications heavily use Supplier for:
- Bean creation
- Lazy loading
- Reactive programming
- Asynchronous pipelines
- Configuration generation
Spring Boot Example
Supplier<User> userSupplier =
User::new;
Supplier in Microservices
Microservices architectures use Supplier for:
- Event generation
- Reactive streams
- Cloud-native lazy loading
- Distributed object creation
- On-demand computations
Microservice Flow
Service Request
|
v
Supplier Generates Data
|
v
Response Returned
Specialized Supplier Interfaces
| Interface | Purpose |
|---|---|
| IntSupplier | Supplies int |
| LongSupplier | Supplies long |
| DoubleSupplier | Supplies double |
| BooleanSupplier | Supplies boolean |
IntSupplier Example
IntSupplier random =
() -> 100;
System.out.println(
random.getAsInt()
);
Output
100
Advantages of Supplier
- Supports lazy initialization
- Reusable generation logic
- Functional programming support
- Easy integration with streams
- Cleaner object creation
Disadvantages
- No input parameter support
- Complex suppliers reduce readability
- Improper lazy loading may affect debugging
Common Interview Mistake
Many developers think Supplier accepts input.
Actually:
- Supplier never accepts input.
Another Common Mistake
Many developers confuse Supplier and Consumer.
Actually:
- Supplier generates data.
- Consumer consumes data.
Supplier vs Consumer
| Feature | Supplier | Consumer |
|---|---|---|
| Input | No Input | Takes Input |
| Output | Returns Value | No Return |
| Main Method | get() | accept() |
| Purpose | Generate Data | Consume Data |
Best Practices
- Use Supplier for lazy initialization
- Keep suppliers simple
- Use method references where possible
- Avoid expensive operations inside suppliers
- Prefer immutable generated objects
- Use specialized suppliers for primitives
Realtime Enterprise Example
Global Analytics Platform
Dashboard Request
|
v
Supplier Generates Real-Time Metrics
|
v
Analytics Dashboard Updated
Related Learning Topics
- What is Functional Programming in Java
- What is Predicate Functional Interface
- What is Consumer Functional Interface
- What is Stream API in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
Supplier is a predefined functional interface available in the java.util.function package introduced in Java 8. It represents a function that does not accept any input parameters but produces and returns a result. The main abstract method of Supplier is get(), which is commonly used for lazy initialization, object creation, data generation, configuration loading, and stream generation. Supplier is heavily used in Stream.generate(), reactive programming, asynchronous pipelines, lazy loading, caching systems, and distributed event-driven architectures. Enterprise applications, Spring Boot systems, banking platforms, distributed microservices, analytics engines, cloud-native architectures, and e-commerce systems extensively use Supplier for scalable object generation, lazy computations, event generation, and reactive workflows. Modern Java development combines Supplier with Stream API, lambda expressions, functional interfaces, reactive programming, Kafka, RabbitMQ, and microservices architectures to build scalable and maintainable enterprise applications.
Frequently Asked Questions
What is Supplier functional interface in Java?
Supplier is a functional interface used to generate and return data without taking any input.
Which package contains Supplier?
java.util.function
What is the main method in Supplier?
get()
Does Supplier take input parameters?
No, Supplier does not accept any input.
Where is Supplier used?
Lazy initialization, Stream.generate(), object creation, Spring Boot applications, and enterprise Java systems.