6.9 Stream Intermediate Operations (Week 6 • Modern Java 21)
1. Core Concept & Internal Working
Stream Intermediate Operations (Modern Java 21) covers `filter`, `map`, `mapToDouble`, `flatMap`, `sorted`, `distinct`, `limit`, `skip`, and `peek`. In production software engineering, it is used to Filtering active employees by salary, flattening nested order line-items with `flatMap`, and paginating in-memory slices. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Filtering active employees by salary, flattening nested order line-items with `flatMap`, and paginating in-memory slices.
3. Granular Subtopics Covered
- `filter`: Core foundation of Stream Intermediate Operations: Filtering active employees by salary, flattening nested order line-items with `flatMap`, and paginating in-memory slices.
- `map`: Master '`map`' inside Stream Intermediate Operations across real-world inputs.
- `mapToDouble`: Master '`mapToDouble`' inside Stream Intermediate Operations across real-world inputs.
- `flatMap`: Master '`flatMap`' inside Stream Intermediate Operations across real-world inputs.
- Production Pitfall Guard (Stream Intermediate Operations): Prevent common defect: Mutating external state inside `peek()` or confusing one-to-one `map` with one-to-many `flatMap`.
- Hands-On Verification (Stream Intermediate Operations): Filter employees by salary threshold and flatten `List<Department>` -> `List<Skill>` using `flatMap`.
4. Reference Implementation
var uniqueSkills = departments.stream()
.flatMap(dept -> dept.employees().stream())
.flatMap(emp -> emp.skills().stream())
.distinct()
.sorted()
.toList();
5. Common Mistakes to Avoid
- Mutating external state inside `peek()` or confusing one-to-one `map` with one-to-many `flatMap`.
- Skipping boundary, null/empty, or error-state verification when implementing Stream Intermediate Operations.
- Pushing untested Stream Intermediate Operations changes without clean naming and Git commit context.
6. Hands-On Exercise
Filter employees by salary threshold and flatten `List<Department>` -> `List<Skill>` using `flatMap`.
7. Technical Interview Checkpoints
- Q: What problem does Stream Intermediate Operations solve in Modern Java 21, and what fails if it is misused?
Key Focus: Filtering active employees by salary, flattening nested order line-items with `flatMap`, and paginating in-memory slices. Guard against: Mutating external state inside `peek()` or confusing one-to-one `map` with one-to-many `flatMap`. - Q: Explain the internal working of Stream Intermediate Operations (`filter`, `map`, `mapToDouble`, `flatMap`, `sorted`, `distinct`, `limit`, `skip`, and `peek`).
Key Focus: Walk step-by-step through `filter`, `map`, `mapToDouble`, `flatMap`, `sorted`, `distinct`, `limit`, `skip`, and `peek` with a concrete code example. - Q: How did you apply and verify Stream Intermediate Operations in your hands-on lab?
Key Focus: Filter employees by salary threshold and flatten `List<Department>` -> `List<Skill>` using `flatMap`.