Commit ceab40b
committed
feat(CombinedExampleALL): demonstrate Predicate, Function, Consumer, and Supplier together
What
- Added CombinedExampleALL class showing usage of all four core functional interfaces from java.util.function:
- Predicate<Integer>: checks if number is even.
- Function<Integer, Integer>: squares an integer.
- Consumer<Integer>: prints an integer to stdout.
- Supplier<Integer>: provides the constant 98.
- Program flow:
- Supplier provides value 98.
- Predicate tests if it is even.
- If true, Function squares the value.
- Consumer prints the result (9604).
Why
- Demonstrates how Java’s functional interfaces can be combined into a workflow.
- Provides an educational example of composing Supplier, Predicate, Function, and Consumer in sequence.
- Helps developers see how each interface fits into functional programming pipelines.
How
- Declared Predicate<Integer> predicate = x -> x % 2 == 0.
- Declared Function<Integer, Integer> function = x -> x * x.
- Declared Consumer<Integer> consumer = x -> System.out.println(x).
- Declared Supplier<Integer> supplier = () -> 98.
- Used supplier.get() to fetch value.
- Applied predicate.test(value) to check condition.
- If true, applied function to transform value, then consumer to output.
Logic
- Inputs: no external input; Supplier always returns 98.
- Outputs: 9604 printed to stdout.
- Flow:
1. supplier.get() → 98.
2. predicate.test(98) → true.
3. function.apply(98) → 9604.
4. consumer.accept(9604) → prints 9604.
- Edge cases:
- If Supplier returned an odd number, Predicate would fail and nothing would print.
- Functions are stateless; no risk of side effects.
- Complexity / performance: O(1) operations; trivial.
- Concurrency / thread-safety:
- Lambdas are stateless and immutable; safe across threads.
- Error handling:
- No exceptions thrown in current code.
Real-life applications
- Predicate for filtering data streams.
- Function for transforming elements in pipelines.
- Consumer for terminal operations like logging or persisting.
- Supplier for lazy value generation, IDs, or configuration defaults.
- Together they form the basis of many Stream API and functional programming patterns.
Notes
- Demonstrates the "four pillars" of functional interfaces in java.util.function.
- Supplier could be replaced with dynamic generation (e.g., random numbers, config values).
- Example is deterministic for clarity, but real-world suppliers often produce changing values.
- This pattern generalizes well to data pipelines: fetch → check → transform → consume.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9f197bf commit ceab40b
File tree
1 file changed
+3
-5
lines changed- Java 8 Crash Course/Lambda Expression/src
1 file changed
+3
-5
lines changedLines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | | - | |
19 | | - | |
| 17 | + | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | | - | |
| 21 | + | |
0 commit comments