Commit e3c4ef2
committed
docs(streams): add explanation and examples for stateful vs stateless operations
What
- Introduced a detailed guide distinguishing **stateless** and **stateful** operations in Java Streams.
- Covered:
1. **Stateless Operations**
- Definition: process each element independently without tracking state.
- Examples: filter(), map(), flatMap(), peek(), forEach().
- Added code demo showing filter + map + forEach pipeline.
- ✅ Output: 4, 8.
2. **Stateful Operations**
- Definition: require tracking previously seen elements or buffering the entire stream.
- Examples: distinct(), sorted(), limit(), skip(), collect(), reduce().
- Added code demo showing distinct + sorted pipeline.
- ✅ Output: 1, 3, 5, 7.
3. **Performance Considerations**
- Stateless = faster, element-by-element, well-suited for parallel execution.
- Stateful = may require memory buffering, reduce efficiency in parallel streams.
4. **Parallel Stream Example**
- Demonstrated how stateless ops work efficiently in parallel, but stateful ops like sorted() may disrupt ordering.
Why
- Many developers use streams without understanding the internal distinction between **stateless (fast, parallel-friendly)** and **stateful (requires global knowledge, potential bottleneck)**.
- Highlighting this difference helps optimize code and avoid performance pitfalls when scaling with parallel streams.
Logic
1. **Stateless operations** → independent per-element processing, no memory of past/future, efficient for parallel.
2. **Stateful operations** → require knowledge of entire dataset (e.g., deduplication, sorting, limiting).
3. Understanding the distinction is crucial:
- For performance tuning (avoid unnecessary stateful ops).
- For correctness (some stateful ops disrupt order in parallel).
4. Code examples illustrate real-world behavior:
- Stateless: doubling even numbers → efficient, streaming pipeline.
- Stateful: removing duplicates + sorting → requires collecting/processing all elements.
Key Takeaways
✔ Stateless ops → filter, map, flatMap, peek, forEach.
✔ Stateful ops → distinct, sorted, limit, skip, collect, reduce.
✔ Stateless = efficient, parallel-friendly.
✔ Stateful = may introduce overhead due to buffering/global knowledge.
✔ Always consider operation type when designing stream pipelines, especially for parallel execution.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9094b77 commit e3c4ef2
File tree
2 files changed
+119
-90
lines changed- JAVA8/StreamsInJAVA/src
- Java 8 Crash Course/Java 8 Streams/Stateful and Stateless Operations/src
2 files changed
+119
-90
lines changedLines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 119 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 | + | |
0 commit comments