Commit 8bfab8c
committed
feat(streams): add comprehensive demo of intermediate operations in Java Streams
What
- Introduced `IntermediateOps.java` to demonstrate all key intermediate operations in Java Streams.
- Covered the following with runnable examples:
1. **filter** → select elements matching a predicate.
2. **map** → transform elements (e.g., to uppercase).
3. **sorted** → sort streams (natural & custom comparator).
4. **distinct** → remove duplicates.
5. **limit** → truncate stream size.
6. **skip** → ignore first N elements.
7. **peek** → side-effect actions for debugging/inspection.
8. **flatMap** → flatten nested collections/arrays into a single stream.
- Included use cases with lists, infinite streams (`Stream.iterate`), nested lists, and sentence splitting.
Why
- Intermediate operations are *lazy* by design: they define transformations but do not execute until a terminal op is called.
- Many developers confuse "pipeline setup" with execution → this example clarifies the difference.
- Demonstrates both stateless (filter, map) and stateful (sorted, distinct) intermediate ops.
Logic
1. **filter**:
- `list.stream().filter(x -> x.startsWith("A"))`
- Builds a pipeline to select elements beginning with "A".
- Returns another stream; execution happens only when a terminal op (like `count`) is called.
2. **map**:
- Transforms elements without changing stream size.
- Example: convert each name to uppercase with `map(String::toUpperCase)`.
3. **sorted**:
- Stateful op → must process all elements before emitting.
- Demonstrated with both natural order and custom comparator (by length).
4. **distinct**:
- Removes duplicate elements based on `equals()`.
- Example: distinct names starting with "A".
5. **limit & skip**:
- Useful with infinite streams (e.g., `Stream.iterate`).
- `limit(100)` produces 100 items.
- `skip(10).limit(100)` starts after skipping first 10.
6. **peek**:
- Provides visibility into stream pipeline for debugging.
- Example: printing elements as they flow through `skip` and `limit`.
7. **flatMap**:
- Flattens nested collections (`List<List<String>>`) into a single stream.
- Used to:
- Convert list-of-lists of fruits into a flat uppercase list.
- Split sentences into words and process all words in a single stream.
Real-world applications
- **Data cleaning pipelines**: filter + map + distinct for deduplication and transformations.
- **Pagination logic**: skip + limit for chunked processing.
- **Debugging ETL pipelines**: peek to log intermediate results.
- **Natural language processing**: flatMap sentences → words for indexing or analytics.
- **Sorting/grouping**: sorted as preparation step for reporting dashboards.
Notes
- Demonstrates how intermediate ops *compose* to build pipelines.
- Reinforces that *no actual computation happens until a terminal operation is invoked*.
- Aimed as both a runnable tutorial and reference example for declarative stream processing.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e74b72c commit 8bfab8c
File tree
2 files changed
+67
-61
lines changed- JAVA8/StreamsInJAVA/src
- Java 8 Crash Course/Java 8 Streams/Intermediate and Terminal Operations/src
2 files changed
+67
-61
lines changedThis file was deleted.
Lines changed: 67 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 | + | |
0 commit comments