How does a Stream work: intermediate vs terminal operations, laziness, and map vs flatMap?
Short answer
A stream has a source, intermediate steps (filter, map) that are only recorded, and one terminal step (toList, count) that actually runs it. That is laziness.
3 points to say:
- Without a terminal step, nothing runs.
mapturns 1 into 1,flatMapturns 1 into many and flattens.- A stream can be used only once.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.