Commit 6adda80
committed
feat(SupplierDemo): add demo using Supplier functional interface
What
- Added SupplierDemo class to demonstrate java.util.function.Supplier.
- Declared Supplier<String> giveHelloWorld returning constant string "Hello World".
- Invoked get() method and printed result.
Why
- Shows how Supplier represents a provider of values without taking input parameters.
- Demonstrates simplest use case: returning a fixed value.
- Educational example introducing Supplier in functional programming context.
How
- Created lambda expression () -> "Hello World" implementing Supplier<String>.
- Called giveHelloWorld.get() to retrieve value.
- Printed "Hello World" to stdout.
Logic
- Inputs: none (Supplier takes no arguments).
- Outputs: "Hello World".
- Flow:
1. Supplier initialized with lambda returning string.
2. get() called on supplier.
3. Value printed to console.
- Edge cases: none in this case since value is constant.
- Complexity / performance: O(1) operation.
- Concurrency / thread-safety: Supplier here is stateless and thread-safe.
- Error handling: no exceptions expected.
Real-life applications
- Delayed/lazy computation of values (on-demand).
- Supplying default values in case of missing data.
- Used with Optional.orElseGet(Supplier) for fallback logic.
- Common in factory methods or configuration loaders.
Notes
- Example demonstrates constant supplier; in real scenarios, Supplier often generates dynamic values (UUIDs, timestamps, random numbers).
- Suppliers are building blocks for lazy evaluation in functional pipelines.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3ca0b1d commit 6adda80
1 file changed
+1
-5
lines changedLines changed: 1 addition & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | 1 | | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
8 | | - | |
9 | 5 | | |
10 | 6 | | |
11 | 7 | | |
12 | | - | |
| 8 | + | |
0 commit comments