🌊 Section 8 · 29 questions
Stream API
29 interview questions and answers in the Stream API section.
Questions in this section
- What advantages does Stream API provide
- What is the difference between intermediate and terminal operations
- What does filter() operation do
- What does map() operation do
- What does collect() operation do
- What is Collector and what built-in Collectors exist
- What does flatMap() operation do
- What is the difference between map() and flatMap()
- What are parallel streams
- When to use parallel streams
- How to create a parallel stream
- What potential problems can occur with parallel streams
- What is ForkJoinPool and how is it related to parallel streams
- Can you modify state of external variables in Stream operations
- What are side effects in Stream
- Why you should avoid side effects in Stream
- What does reduce() operation do
- What is the difference between reduce() and collect()
- What is peek() operation and when to use it
- Can you reuse a Stream
- What is lazy evaluation in Stream
- When does Stream operation execution begin
- What do distinct(), sorted(), limit(), skip() operations do
- How does short-circuiting work in Stream
- What are anyMatch(), allMatch(), noneMatch() operations
- What do findFirst() and findAny() operations do
- How to collect Stream into Map
- What to do about key collisions when collecting into Map
- How to work with Optional in Stream
Study navigator
A comprehensive guide containing 29 curated interview questions and deep-dive technical analyses on the Java Stream API, functional pipelines, Spliterators, and parallel processing.
📑 Complete Question Index
| # | Question | Difficulty | Core Topics |
|---|---|---|---|
| 01 | What advantages does Stream API provide | 🟢 Junior | Declarative vs Imperative, Internal Iteration, Fusion |
| 02 | What is the difference between intermediate and terminal operations | 🟢 Junior | Stateless vs Stateful, Lazy Pipelines, Execution Triggers |
| 03 | What does filter() operation do | 🟢 Junior | Predicate<T>, Stateless Filtering, Immutability |
| 04 | What does map() operation do | 🟢 Junior | Function<T, R>, 1-to-1 Transformations, Type Evolution |
| 05 | What does collect() operation do | 🟡 Middle | Mutable Reduction, Collector<T, A, R>, .toList() (Java 16+) |
| 06 | What is Collector and what built-in Collectors exist | 🟡 Middle | groupingBy, partitioningBy, toMap, Downstream Collectors |
| 07 | What does flatMap() operation do | 🟡 Middle | 1-to-N Flattening, Monadic Composition, Nested Streams |
| 08 | What is the difference between map() and flatMap() | 🟡 Middle | Structure Preserving vs Flattening, Dimensionality |
| 09 | What are parallel streams | 🔴 Senior | ForkJoinPool, Spliterator Splitting, Multi-Core Processing |
| 10 | When to use parallel streams | 🔴 Senior | $N \times Q > 10{,}000$ Heuristic, CPU-Bound vs I/O-Bound Work |
| 11 | How to create a parallel stream | 🟡 Middle | .parallel(), Collection.parallelStream(), Schedulers |
| 12 | What potential problems can occur with parallel streams | 🔴 Senior | Thread Contention, Common Pool Exhaustion, Memory Overhead |
| 13 | What is ForkJoinPool and how is it related to parallel streams | 🔴 Senior | Work-Stealing, commonPool(), Custom Pool Execution |
| 14 | Can you modify state of external variables in Stream operations | 🟡 Middle | Effectively Final Invariant, Race Conditions, Thread Safety |
| 15 | What are side effects in Stream | 🟡 Middle | Pure Functions, Non-Interference, Mutating External State |
| 16 | Why you should avoid side effects in Stream | 🟡 Middle | Concurrency Hazards, Pipeline Reordering, Broken Pipelines |
| 17 | What does reduce() operation do | 🟡 Middle | Identity, Accumulator, Combiner, Mathematical Folding |
| 18 | What is the difference between reduce() and collect() | 🟡 Middle | Immutable vs Mutable Reduction, Allocation Overhead |
| 19 | What is peek() operation and when to use it | 🟡 Middle | Diagnostic Logging, JIT Optimization Skipping, Side Effect Trap |
| 20 | Can you reuse a Stream | 🟢 Junior | Single-Use Lifecycle, IllegalStateException, Supplier Factory |
| 21 | What is lazy evaluation in Stream | 🟡 Middle | Pipeline Construction, Loop Fusion, Deferred Execution |
| 22 | When does Stream operation execution begin | 🟢 Junior | Terminal Operation Trigger, Single-Pass Traversals |
| 23 | What do distinct(), sorted(), limit(), skip() operations do | 🟡 Middle | Stateful Operations, Memory Buffering, Spliterator Flags |
| 24 | How does short-circuiting work in Stream | 🟡 Middle | Early Termination, Infinite Streams, findFirst, anyMatch |
| 25 | What are anyMatch(), allMatch(), noneMatch() operations | 🟢 Junior | Predicate Matching, Boolean Reductions, Vacuous Truth |
| 26 | What do findFirst() and findAny() operations do | 🟡 Middle | Determinism vs Parallelism, Encounter Order, Optional<T> |
| 27 | How to collect Stream into Map | 🟡 Middle | Collectors.toMap, Key & Value Mappers, Map Factories |
| 28 | What to do about key collisions when collecting into Map | 🔴 Senior | Merge Functions, BinaryOperator, Duplicate Resolution |
| 29 | How to work with Optional in Stream | 🟡 Middle | Optional.stream(), Flattening Optionals, Unwrapping Patterns |
🗺️ Topic Dependency & Flow Graph
┌────────────────────────────────────────────────────────┐
│ STREAM FOUNDATIONS │
│ 01. Advantages & Declarative Model │
│ 02. Intermediate vs. Terminal Operations │
│ 20. Single-Use Stream Lifecycle │
│ 21. Lazy Evaluation & 22. Execution Trigger │
└───────────────────────────┬────────────────────────────┘
│
┌───────────────────────────┴────────────────────────────┐
▼ ▼
┌────────────────────────────────────────────────────────┐ ┌────────────────────────────────────────────────────────┐
│ CORE TRANSFORMATIONS │ │ COLLECTORS & REDUCTION │
│ 03. filter() & 04. map() │ │ 05. collect() & 06. Built-in Collectors │
│ 07. flatMap() & 08. map vs flatMap │ │ 17. reduce() & 18. reduce vs collect │
│ 23. distinct(), sorted(), limit(), skip() │ │ 27. toMap Collection & 28. Key Collision Resolution │
│ 24. Short-Circuiting & 25. Matchers (any/all/none) │ │ 29. Optional Integration in Streams │
│ 26. findFirst() vs findAny() │ │ │
└────────────────────────────┬───────────────────────────┘ └───────────────────────────┬────────────────────────────┘
│ │
└───────────────────────────┬──────────────────────────────┘
│
┌───────────────────────────┴────────────────────────────┐
▼ ▼
┌────────────────────────────────────────────────────────┐ ┌────────────────────────────────────────────────────────┐
│ CONCURRENCY & SIDE EFFECTS │ │ PARALLEL STREAMS & INTERNALS │
│ 14. Modifying External State Invariant │ │ 09. Parallel Stream Mechanics │
│ 15. Side Effects Definition │ │ 10. When to Use Parallel Streams ($N \times Q$) │
│ 16. Why to Avoid Side Effects │ │ 11. Creating Parallel Streams │
│ 19. peek() Pitfalls & JIT Skipping │ │ 12. Parallelism Pitfalls & Thread Contention │
│ │ │ 13. ForkJoinPool & Work-Stealing Internals │
└────────────────────────────────────────────────────────┘ └────────────────────────────────────────────────────────┘
🎯 Recommended Study Tracks
🟢 Junior Track (Stream Basics & Core Functions)
- What advantages does Stream API provide
- What is the difference between intermediate and terminal operations
- What does filter() operation do
- What does map() operation do
- Can you reuse a Stream
- When does Stream operation execution begin
- What are anyMatch(), allMatch(), noneMatch() operations
🟡 Middle Track (Collectors, Flattening & Reductions)
- What does collect() operation do
- What is Collector and what built-in Collectors exist
- What does flatMap() operation do
- What is the difference between map() and flatMap()
- What does reduce() operation do
- What is the difference between reduce() and collect()
- What is lazy evaluation in Stream
- What do distinct(), sorted(), limit(), skip() operations do
- How to collect Stream into Map
- How to work with Optional in Stream
🔴 Senior Track (Parallelism, Spliterators & Concurrency)
- What are parallel streams
- When to use parallel streams
- What potential problems can occur with parallel streams
- What is ForkJoinPool and how is it related to parallel streams
- Why you should avoid side effects in Stream
- What is peek() operation and when to use it
- What to do about key collisions when collecting into Map