The Stream API (java.util.stream) provides a functional, declarative approach to processing sequences of elements. Streams do not store data; they operate on a data source (collection, array, I/O channel) and produce a result without mutating the original source. Stream Pipeline Architecture A Stream pipeline consists of three core phases: Source: Supplies elements (e.g., list.stream(), Stream.of(), IntStream.range()). Intermediate Operations: Return a new stream and are lazy — no execution occurs until a terminal operation is called. Terminal Operations: Trigger pipeline execution and produce a non-stream result (List, Map, Integer, void). Once a terminal operation is called, the stream is consumed and cannot be reused. Key Intermediate Operations Operation Type Purpose Example…
Key Takeaways
Streams are declarative, immutable pipelines over data sources.
Intermediate operations are lazy and chained; terminal operations execute.
A stream cannot be reused once consumed by a terminal operation.
Use flatMap to flatten one-to-many nested relationships.
Use primitive streams (IntStream, LongStream, DoubleStream) to avoid autoboxing overhead.