🌊 Section 8 · 29 questions

Stream API

29 interview questions and answers in the Stream API section.

Questions in this section

  1. What advantages does Stream API provide
  2. What is the difference between intermediate and terminal operations
  3. What does filter() operation do
  4. What does map() operation do
  5. What does collect() operation do
  6. What is Collector and what built-in Collectors exist
  7. What does flatMap() operation do
  8. What is the difference between map() and flatMap()
  9. What are parallel streams
  10. When to use parallel streams
  11. How to create a parallel stream
  12. What potential problems can occur with parallel streams
  13. What is ForkJoinPool and how is it related to parallel streams
  14. Can you modify state of external variables in Stream operations
  15. What are side effects in Stream
  16. Why you should avoid side effects in Stream
  17. What does reduce() operation do
  18. What is the difference between reduce() and collect()
  19. What is peek() operation and when to use it
  20. Can you reuse a Stream
  21. What is lazy evaluation in Stream
  22. When does Stream operation execution begin
  23. What do distinct(), sorted(), limit(), skip() operations do
  24. How does short-circuiting work in Stream
  25. What are anyMatch(), allMatch(), noneMatch() operations
  26. What do findFirst() and findAny() operations do
  27. How to collect Stream into Map
  28. What to do about key collisions when collecting into Map
  29. 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            │
  └────────────────────────────────────────────────────────┘  └────────────────────────────────────────────────────────┘

🟢 Junior Track (Stream Basics & Core Functions)

  1. What advantages does Stream API provide
  2. What is the difference between intermediate and terminal operations
  3. What does filter() operation do
  4. What does map() operation do
  5. Can you reuse a Stream
  6. When does Stream operation execution begin
  7. What are anyMatch(), allMatch(), noneMatch() operations

🟡 Middle Track (Collectors, Flattening & Reductions)

  1. What does collect() operation do
  2. What is Collector and what built-in Collectors exist
  3. What does flatMap() operation do
  4. What is the difference between map() and flatMap()
  5. What does reduce() operation do
  6. What is the difference between reduce() and collect()
  7. What is lazy evaluation in Stream
  8. What do distinct(), sorted(), limit(), skip() operations do
  9. How to collect Stream into Map
  10. How to work with Optional in Stream

🔴 Senior Track (Parallelism, Spliterators & Concurrency)

  1. What are parallel streams
  2. When to use parallel streams
  3. What potential problems can occur with parallel streams
  4. What is ForkJoinPool and how is it related to parallel streams
  5. Why you should avoid side effects in Stream
  6. What is peek() operation and when to use it
  7. What to do about key collisions when collecting into Map