Commit 6ad5f4c
committed
feat(streams): add comprehensive demo of terminal operations in Java Streams
What
- Introduced `TerminalOps.java` to showcase all major terminal operations in Java Streams.
- Implemented examples with `List`, `Stream`, and Strings to demonstrate:
1. **collect** → gather results into collections (`toList`, `Collectors.toList`).
2. **forEach / forEachOrdered** → process elements sequentially or with order guarantees.
3. **reduce** → accumulate elements into a single result (e.g., sum).
4. **count** → count number of elements in stream.
5. **anyMatch / allMatch / noneMatch** → short-circuiting predicate-based checks.
6. **findFirst / findAny** → retrieve elements, important for sequential vs parallel streams.
7. **toArray** → convert stream elements into an array.
8. **min / max** → find smallest or largest element with comparators.
9. **parallel stream behavior** → difference between `forEach` and `forEachOrdered`.
- Added multiple applied use cases:
- Filtering and collecting names by length.
- Squaring and sorting numbers.
- Summing integers via `reduce`.
- Counting occurrences of a character in a String (`chars()` stream).
- Demonstrating **stream reuse limitation** (consumed stream cannot be reused).
Why
- Terminal operations are essential because they **trigger execution** of stream pipelines.
- Without a terminal operation, intermediate ops remain lazy and never execute.
- This file provides a consolidated tutorial for developers to understand:
- Different result types: collections, primitives, optionals.
- Short-circuiting behavior (`anyMatch`, `findFirst`).
- Parallel stream ordering guarantees.
Logic
1. **collect**
- `list.stream().skip(1).collect(Collectors.toList())` → gathers elements into a collection.
- Modern shorthand: `list.stream().skip(1).toList()` (Java 16+).
2. **forEach / forEachOrdered**
- `forEach` executes actions on each element (order not guaranteed in parallel).
- `forEachOrdered` enforces encounter order in parallel streams.
3. **reduce**
- Combines elements (e.g., sum, product, concatenation).
- Returns an `Optional<T>` if identity not provided.
4. **count**
- Efficient terminal op to count elements (long value).
5. **anyMatch / allMatch / noneMatch**
- Predicate checks with short-circuiting (pipeline halts early if result is known).
6. **findFirst / findAny**
- `findFirst`: deterministic (first element in stream).
- `findAny`: may return any element (useful in parallel streams for efficiency).
7. **toArray**
- Collects stream elements into an array (`Object[]` or typed array).
8. **min / max**
- Finds extreme values using a comparator or natural order.
9. **Parallel ordering**
- Showcases how unordered parallel execution differs between `forEach` and `forEachOrdered`.
10. **Stream reuse limitation**
- Once consumed by a terminal op, a stream cannot be reused → attempting leads to `IllegalStateException`.
Real-world applications
- **Aggregation pipelines**: Summing, counting, min/max in reporting.
- **Search operations**: Quickly finding first/any matching element.
- **Collections transformation**: Gathering processed data into Lists, Sets, or Maps.
- **Parallelism**: Leveraging short-circuiting and ordering in parallel stream processing.
- **String analysis**: Counting frequency of characters or substrings.
- **Data integrity**: Teaching stream reuse rules prevents runtime exceptions.
Notes
- Complements `IntermediateOps.java`: together, they cover full lifecycle of stream pipelines.
- Emphasizes **lazy vs eager** evaluation boundary.
- Acts as both runnable examples and a learning reference for terminal stream operations.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8bfab8c commit 6ad5f4c
File tree
1 file changed
+29
-31
lines changed- Java 8 Crash Course/Java 8 Streams/Intermediate and Terminal Operations/src
1 file changed
+29
-31
lines changedLines changed: 29 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | | - | |
| 12 | + | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
16 | 15 | | |
17 | | - | |
| 16 | + | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
21 | 20 | | |
22 | | - | |
23 | | - | |
24 | | - | |
| 21 | + | |
25 | 22 | | |
26 | | - | |
| 23 | + | |
| 24 | + | |
27 | 25 | | |
28 | | - | |
| 26 | + | |
| 27 | + | |
29 | 28 | | |
30 | | - | |
| 29 | + | |
31 | 30 | | |
32 | | - | |
| 31 | + | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
40 | 37 | | |
| 38 | + | |
41 | 39 | | |
42 | 40 | | |
43 | | - | |
| 41 | + | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | | - | |
| 45 | + | |
48 | 46 | | |
49 | 47 | | |
50 | | - | |
| 48 | + | |
51 | 49 | | |
52 | | - | |
53 | | - | |
| 50 | + | |
54 | 51 | | |
55 | 52 | | |
56 | 53 | | |
| |||
65 | 62 | | |
66 | 63 | | |
67 | 64 | | |
68 | | - | |
| 65 | + | |
69 | 66 | | |
70 | | - | |
| 67 | + | |
71 | 68 | | |
72 | | - | |
| 69 | + | |
73 | 70 | | |
74 | 71 | | |
75 | 72 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 73 | + | |
81 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
82 | 80 | | |
83 | | - | |
| 81 | + | |
0 commit comments