Stream API
The standard-library toolkit for describing a sequence of data-processing steps, such as filter, map and collect, over a source like a list.
In more detail
A stream pipeline has a source, zero or more lazy intermediate operations, and one terminal operation that starts the work and produces a result. The source collection is not changed. Streams are a vocabulary for data flow, not a replacement for every loop, and callbacks inside them should avoid side effects.
Example
Stream.of("Ada", "Bob", "Ana").filter(n -> n.startsWith("A")).map(String::toUpperCase).toList()Related terms
- intermediate operation — A stream step, such as filter, map, sorted or distinct, that returns another stream and does no work until a terminal operation runs.
- terminal operation — The final stream step, such as toList, collect, count or anyMatch, that starts the pipeline and produces a result.
- lazy evaluation — Postponing work until its result is actually needed, rather than doing it all in advance.
- collector — A recipe passed to a stream's collect operation that gathers the elements into a result, such as a set or a map grouped by key.
- lambda expression — A short anonymous function written inline and passed to other code, such as a condition for a filter.
Taught in
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- The Streams API — Modern Java: Virtual Threads & High-Throughput Services
- Advanced Stream Operations — Modern Java: Virtual Threads & High-Throughput Services
- Stream Pipelines, Primitive Streams And Optional — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep