Intermediate vs Terminal Operations and Laziness
35 min · Intermediate
A stream pipeline is a source, zero or more intermediate operations and exactly one terminal operation. Interviewers ask about it because one idea, laziness, explains most stream puzzles: nothing runs until the terminal operation asks for data. Section 8, Stream Basics, taught this in full; this lesson is the interview revision.
Topics in this lesson
- The pipeline: source, intermediate, terminal
- Laziness: nothing runs without a terminal operation
- Short-circuiting: findFirst, anyMatch, limit
- Streams are single-use (Collection vs Stream)
- Traps: side effects, changing the source, parallel streams
Interview questions this lesson answers
- What is the difference between intermediate and terminal operations?
- What does it mean that streams are lazy, and why is it useful?
- Can you reuse a stream? What happens if you try?
- How is a Stream different from a Collection?
- Your
peekwith a print statement shows nothing. Why could that happen? - When would you use a parallel stream, and when would you avoid it?
- Why should the lambda inside
maphave no side effects? - What is a stream in Java?
- Name some ways to create a stream.
- Which operations are intermediate? Name at least five.
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.