Commit d6c607a
committed
feat(FunctionalProgramming): add Main demonstrating streams with filter and sum
What
- Added Main class in package FunctionalProgramming.
- Demonstrates Java Streams API with filter, mapToInt, and sum.
- Key operations:
- Created numbers list with integers 1 to 10.
- First filter (x -> x > 100000) without terminal operation — no effect (lazy evaluation).
- Second pipeline: filter even numbers, convert to IntStream, compute sum.
- Printed result (30).
Why
- Example shows functional programming with streams instead of loops.
- Demonstrates how filters and aggregations are composed in pipelines.
- Highlights lazy evaluation: without terminal operation, intermediate steps don’t execute.
- Provides educational example of filtering and aggregating with concise code.
How
- Declared numbers = Arrays.asList(1…10).
- Called numbers.stream().filter(x -> x > 100000); (unused, no terminal op).
- Created second stream:
- filter(n -> n % 2 == 0) → selects even numbers.
- mapToInt(n -> n) → converts to IntStream.
- sum() → aggregates total.
- Printed sum.
Logic
- Inputs: list [1,2,3,4,5,6,7,8,9,10].
- Outputs: integer 30 printed to stdout.
- Flow:
1. Build list of integers.
2. First stream + filter defined but no terminal op → no output, no execution.
3. Second stream pipeline executes:
- Filter → [2,4,6,8,10].
- MapToInt → [2,4,6,8,10] (IntStream).
- sum() → 30.
4. Print 30.
- Edge cases:
- If list is empty, sum() returns 0 safely.
- First filter is dead code since not consumed.
- Complexity / performance: O(n) for stream traversal; efficient for small lists.
- Concurrency / thread-safety:
- List is immutable (from Arrays.asList).
- Stream operations are stateless and safe here.
- Error handling: no exceptions expected.
Real-life applications
- Calculating aggregates (sum, average) with concise stream pipelines.
- Filtering datasets with conditions (even numbers, IDs > threshold).
- Functional programming style avoids explicit loops and mutable accumulators.
- Basis for scalable data processing (stream pipelines can be parallelized).
Notes
- To make first filter effective, add terminal operation like forEach or collect.
- mapToInt(n -> n) can be simplified to mapToInt(Integer::intValue).
- Example shows laziness of streams — key concept in functional programming.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 32a0999 commit d6c607a
File tree
1 file changed
+42
-0
lines changed- Java 8 Crash Course/Predicate/src/FunctionalProgramming
1 file changed
+42
-0
lines changedLines changed: 42 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments