Skip to content

Commit 6adda80

Browse files
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

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import java.util.function.Consumer;
2-
import java.util.function.Function;
3-
import java.util.function.Predicate;
41
import java.util.function.Supplier;
52

63
public class SupplierDemo {
74
public static void main(String[] args) {
8-
//This is used in Database connectivity.
95
Supplier<String> giveHelloWorld = () -> "Hello World";
106
System.out.println(giveHelloWorld.get());
117
}
12-
}
8+
}

0 commit comments

Comments
 (0)