Skip to content

Commit ceab40b

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

1 file changed

+3
-5
lines changed

JAVA8/JAVA8FeaturesUsedInStreams/src/combinedExampleALL.java renamed to Java 8 Crash Course/Lambda Expression/src/CombinedExampleALL.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import java.util.function.Predicate;
44
import java.util.function.Supplier;
55

6-
public class combinedExampleALL {
6+
public class CombinedExampleALL {
77
public static void main(String[] args) {
88
// combined example
9-
109
Predicate<Integer> predicate = x -> x % 2 == 0;
1110

1211
Function<Integer, Integer> function = x -> x * x;
@@ -15,9 +14,8 @@ public static void main(String[] args) {
1514

1615
Supplier<Integer> supplier = () -> 98;
1716

18-
if (predicate.test(supplier.get()))
19-
{
17+
if (predicate.test(supplier.get())) {
2018
consumer.accept(function.apply(supplier.get()));
2119
}
2220
}
23-
}
21+
}

0 commit comments

Comments
 (0)