The java.util.stream.Stream interface, added in Java 8, represents a sequence of elements that you process with a declarative pipeline of operations such as filter, map and collect. It matters because it separates what you want from how it is iterated, evaluates lazily, and can switch to parallel execution with one call. A stream is not a data structure: it does not store elements, and it can be consumed only once.
Streams reached their current form over several releases, and a few recent additions (toList(), mapMulti, and stream gatherers) remove much of the older boilerplate.
Every pipeline has three parts: a source, zero or more intermediate operations, and one terminal operation.
list.stream() or Stream.of(...).filter, map, sorted and distinct return a new stream and do no work yet.collect, forEach, count, reduce or findFirst, starts the traversal and produces a result.Stream.toList() replaced the older collect(Collectors.toList()) for most cases. Note that the list it returns cannot be modified.
Intermediate operations run only when a terminal operation asks for elements, and each element flows through the whole pipeline before the next one starts. Short-circuiting operations such as findFirst, anyMatch and limit stop early.
Only two elements are examined. This is why a stream over a large or infinite source (Stream.iterate, Stream.generate) can still finish quickly when you add a limit or a takeWhile.
Stream<Integer> boxes every value. For numeric work, use IntStream, LongStream and DoubleStream, which avoid boxing and add sum(), average() and summaryStatistics().
Use flatMap when each element maps to several results, for example turning a list of orders into a stream of order lines. mapMulti (Java 16) does the same imperatively when creating small intermediate streams would be wasteful.
parallelStream() or .parallel() splits the work across the common fork-join pool. It helps only when the source splits cheaply (arrays, ArrayList), the per-element work is CPU-heavy and independent, and there is enough data. It can make things slower for small inputs, linked lists, blocking I/O, or operations with shared mutable state.
Rules for safe parallel use:
forEach; use collect.| Feature | Since | What it gives you |
|---|---|---|
takeWhile, dropWhile, Stream.iterate with predicate | Java 9 | Conditional slicing and finite generators |
Stream.toList() | Java 16 | Short unmodifiable list collection |
mapMulti | Java 16 | Emit zero or more results per element without nested streams |
Stream gatherers (Stream.gather) | Java 24 | Custom intermediate operations such as windowing and folding |
Gatherers let you write operations like fixed-size windows through java.util.stream.Gatherers, something the built-in operations could not express.
IllegalStateException.peek for real work. It exists for debugging and may be skipped.java.util.stream.Stream with java.io streams such as InputStream. They are unrelated.A collection stores elements and can be traversed repeatedly. A stream describes a computation over a source, is consumed once, and is evaluated lazily.
Not by default. Sequential streams are usually on par with loops, sometimes slightly slower for tiny inputs. Their value is readability and easy parallelism, so benchmark with a tool such as JMH before claiming a speedup.
map turns each element into exactly one result. flatMap turns each element into a stream and flattens all those streams into one.
Use toList() on Java 16 and later if an unmodifiable result is fine. Use Collectors.toCollection(ArrayList::new) when you need a mutable list.
Stream is Java's abstraction for functional-style, lazily evaluated processing of element sequences. Build a pipeline from a source, intermediate operations and one terminal operation, prefer primitive streams for numbers, and use parallel streams only after measuring. Modern additions such as toList(), mapMulti and gatherers keep pipelines short and expressive.