Commit 19ea880
committed
feat(streams): add EvaluationStreams demo to illustrate lazy vs eager execution in Java Streams
What
- Introduced `EvaluationStreams.java` with a clear example contrasting intermediate and terminal operations.
- Demonstrated how **filter()** (intermediate op) sets up a pipeline without executing immediately.
- Added debug prints inside filter to visualize when evaluation actually occurs.
- Showed lifecycle:
1. Stream creation with `filter()`.
2. Lazy setup → no filtering happens before terminal op.
3. Execution triggered by `collect()` (terminal op).
4. Stream consumed → cannot be reused.
Why
- Developers often misunderstand why nothing happens when only intermediate operations are chained.
- This demo clarifies:
- Intermediate ops = **lazy** (build pipeline, no execution).
- Terminal ops = **eager** (trigger evaluation, consume stream).
- Provides a teaching example for the evaluation strategy of Java Streams.
Logic
1. **Stream creation**
- `names.stream().filter(...)` defines a pipeline.
- filter has logic (`name.length() > 3`) with side-effect print `"Filtering: <name>"`.
- No output yet because evaluation is deferred.
2. **Before terminal operation**
- Printed message `"Before terminal operation"`.
- Confirms that intermediate ops didn’t run yet.
3. **Terminal operation (`collect`)**
- `collect(Collectors.toList())` executes the pipeline.
- At this point, filtering executes sequentially for each element:
```
Filtering: Alice
Filtering: Bob
Filtering: Charlie
Filtering: David
```
- Result: `["Alice", "Charlie", "David"]`.
4. **After terminal operation**
- Printed `"After terminal operation"` and result list.
- Demonstrates consumption → stream is closed and cannot be reused.
Real-world applications
- Debugging pipelines: printing inside filters/peeks to trace lazy evaluation.
- Teaching functional/declarative style vs imperative loops.
- Understanding performance benefits: pipelines are only executed when needed.
- Production optimization: allows developers to chain multiple filters/maps without cost until terminal op executes.
Notes
- Intermediate ops: lazy, return new streams (e.g., filter, map, distinct, sorted).
- Terminal ops: eager, consume streams (e.g., collect, forEach, reduce, count).
- Once a terminal operation runs, reusing the same stream throws `IllegalStateException`.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6ad5f4c commit 19ea880
File tree
1 file changed
+128
-0
lines changed- Java 8 Crash Course/Java 8 Streams/Intermediate and Terminal Operations/src
1 file changed
+128
-0
lines changedLines changed: 128 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 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
0 commit comments