Part 4 · Fundamentals

Lambdas, streams and honest parallelism

Prerequisites: 01-ecosystem

When does a stream actually execute?

Source: quotes; filter → map: lazy transformations; toList(): terminal traversal; Result list: materialized values. Connections: Source to filter → map (elements); filter → map to toList() (evaluated on demand); toList() to Result list (collect)
This is a sequential pipeline. Parallel execution requires separate assumptions about side effects and workload. [S08] [S11]

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]
Keep this: Readable transformation beats decorative functional syntax.
Check yourself: Why does filter alone produce no result?

It is intermediate and lazy; a terminal operation such as toList triggers traversal.

Sources & further reading

  1. [S08] Stream API

    Oracle / OpenJDK · documentation · accessed 2026-10-09 · Java SE 25

    Supports: Laziness; Terminal operations; Non-interference

    Read the linked section to validate the mechanism and its version-specific constraints.

  2. [S11] Concurrency utilities

    Oracle / OpenJDK · documentation · accessed 2026-10-09 · Java SE 25

    Supports: Executors and concurrency building blocks

    Read the linked section to validate the mechanism and its version-specific constraints.