Lambdas, streams and honest parallelism
Prerequisites: 01-ecosystem
When does a stream actually execute?
Goal & mental model verified
A lambda implements a functional interface. A Stream describes a computation over a source: intermediate transformations are lazy, and a terminal operation consumes the pipeline. A stream is not a reusable collection.
[S08] [S11]Worked example · illustrative, not executed synthesis
var ids = quotes.stream()
.filter(q -> q.premium().signum() > 0)
.map(Quote::id)
.toList();
// Schematic: Quote exposes premium() and id().[S08] [S11]Engineering decision synthesis
Use streams for clear transformations and loops for stateful control flow or early branching. Parallel execution adds coordination overhead and is useful only with appropriate work and isolation.
[S08] [S11]Pitfall & diagnosis synthesis
Side effects inside map/filter make ordering and parallel behavior difficult to reason about. A parallel stream is not a safe way to issue unbounded database calls.
[S08] [S11]Improve & validate synthesis
Start sequential. Benchmark representative inputs and examine thread usage before adopting parallelism; isolate blocking I/O with an explicit bounded execution policy.
[S08] [S11]Check yourself: Why does filter alone produce no result?
It is intermediate and lazy; a terminal operation such as toList triggers traversal.