Java Streams: Lazy Evaluation and Common Operations
Fast recall
Quick revision
Core answer
Whether you can write idiomatic stream pipelines, explain the lazy-evaluation model, and know which operations are stateful (sorted, distinct) vs stateless (filter, map)
Reusing a stream after it has been consumed, forgetting that streams don't modify the source collection, or calling a terminal operation twice
Explain short-circuit operations (findFirst, anyMatch, limit), distinguish sequential vs parallel streams and when parallelism helps, and describe collectors like groupingBy and joining
Interview-ready guide
Interview answer
Streams are pipelines, not loops — intermediate operations build a recipe; terminal operations execute it.
Lazy evaluation:
.filter().map().sorted()— nothing executes; each call returns a new Stream recording the operation- Terminal operation (
.collect(),.findFirst(),.count()) triggers the pipeline findFirst()on a million-element stream may process only one element — short-circuits immediately on first match
Pipeline barriers:
sorted()must consume all upstream elements before producing any output — it breaks laziness- Place
limit()beforesorted()to limit what gets sorted, not after
Common mistakes:
- Reusing a consumed stream throws
IllegalStateException— streams are single-use; collect to a list first if multiple passes are needed - Side effects in
peek()ormap()are unreliable — intermediate operations can execute out of order or not at all due to short-circuiting; side effects belong only inforEach()
I recommend streams for multi-step transformations where the intent reads like a specification — for simple iteration with one condition, an enhanced for-loop is faster and clearer.
Java Streams: Lazy Evaluation and Common Operations
Direct Answer
A Stream is a declarative pipeline of operations on a data source. Intermediate operations (filter, map, flatMap, sorted) are lazy — they don't execute until a terminal operation (collect, forEach, reduce, count) is called. The Stream then fuses all intermediate steps into a single traversal of the source data.
Common Mistakes & Pitfalls
Reusing a stream after it has been consumed, forgetting that streams don't modify the source collection, or calling a terminal operation twice
Check your understanding
Answer these without looking back at the article.
- What is the difference between map and flatMap?
- When should you use parallel streams?
- How does Collectors.groupingBy work internally?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.