Commit ac95e09
committed
docs(streams): add detailed guide and examples for Java 8 Streams API
What
- Added comprehensive documentation explaining **Java 8 Streams**:
- Definition of a Stream (sequence of elements from collections/arrays/I/O).
- Key characteristics:
* Functional style (lambdas & method references).
* Lazy evaluation (intermediates executed only when terminal ops run).
* Pipeline processing (Source → Intermediate → Terminal).
* No side effects (encourages pure, stateless functions).
* Parallel execution (parallelStream() support).
- Classification of operations:
* Intermediate (filter, map, sorted, etc.) → lazy.
* Terminal (forEach, collect, count, reduce, etc.) → eager, consume the stream.
- Stream workflow steps: creation, transformation, terminal evaluation.
- Added **example programs**:
1. **Basic Filtering**
- Filters names starting with `"A"` and prints them.
- Demonstrates `.filter(...)` + `.forEach(...)`.
```java
names.stream().filter(n -> n.startsWith("A")).forEach(System.out::println);
```
✅ Output: `Alice`.
2. **Collecting Results**
- Filters even numbers into a new `List<Integer>`.
- Uses `.collect(Collectors.toList())`.
✅ Output: `[2, 4]`.
3. **Mapping and Reducing**
- Sums integers using `.reduce(0, Integer::sum)`.
✅ Output: `Sum: 15`.
4. **Parallel Streams**
- Uses `.parallelStream()` to process elements in parallel.
- Demonstrates unordered parallel execution.
Why
- Streams represent a **functional and declarative approach** to data processing in Java 8.
- They simplify common tasks like filtering, mapping, reducing, and collecting into **concise, readable pipelines**.
- Understanding Streams is essential for:
- Backend data manipulation (Spring Boot, microservices).
- Parallel processing of large datasets.
- Replacing verbose imperative loops with functional style.
Logic
1. **Streams don’t store data** → they work over a data source.
2. **Intermediate ops are lazy** → nothing executes until a terminal operation is invoked.
3. **Terminal ops trigger execution** and consume the stream.
4. **Parallel streams** split workload across CPU cores for performance on large independent tasks.
5. **Pipeline processing** allows composition of multiple operations in a single fluent chain.
Key Takeaways
✔ Streams = functional, lazy, declarative.
✔ Intermediate ops = transformations (filter/map/sorted).
✔ Terminal ops = results (collect/reduce/forEach).
✔ Parallel streams = enable concurrency with minimal effort.
✔ Benefits = cleaner code, reduced boilerplate, efficient execution.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 108e4b3 commit ac95e09
1 file changed
+18
-19
lines changedLines changed: 18 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | | - | |
| 4 | + | |
4 | 5 | | |
5 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | | - | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
15 | | - | |
| 18 | + | |
16 | 19 | | |
17 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| |||
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
28 | | - | |
| 33 | + | |
29 | 34 | | |
30 | 35 | | |
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
34 | | - | |
| 39 | + | |
35 | 40 | | |
36 | 41 | | |
37 | 42 | | |
| |||
48 | 53 | | |
49 | 54 | | |
50 | 55 | | |
51 | | - | |
52 | 56 | | |
53 | 57 | | |
54 | | - | |
| 58 | + | |
55 | 59 | | |
56 | 60 | | |
57 | 61 | | |
| |||
70 | 74 | | |
71 | 75 | | |
72 | 76 | | |
73 | | - | |
74 | 77 | | |
75 | 78 | | |
76 | | - | |
| 79 | + | |
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
| |||
90 | 93 | | |
91 | 94 | | |
92 | 95 | | |
93 | | - | |
94 | 96 | | |
95 | 97 | | |
96 | | - | |
97 | | - | |
| 98 | + | |
98 | 99 | | |
99 | 100 | | |
100 | | - | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
114 | | - | |
| 113 | + | |
115 | 114 | | |
116 | 115 | | |
117 | | - | |
| 116 | + | |
0 commit comments