Learn Java
import com.learnjava.advancedjava.StreamsApi;

The Stream API in Java

@Concept

A Stream, from java.util.stream, represents a sequence of elements that can be processed through a pipeline of operations — filter, map, sorted, and similar — chained together, without manually writing the equivalent loops by hand. Stream operations split into two kinds: intermediate operations like filter and map return another Stream and are lazy, meaning they don't actually run any processing yet; a terminal operation like collect, count, or forEach is what actually triggers the whole pipeline to execute, all at once, from start to finish.

A Stream isn't a data structure itself and doesn't store any elements of its own — it's a view over some other source, most often a collection, computed on demand. Once a terminal operation has consumed a Stream, that specific Stream object is done and can't be reused or run through again; a fresh one has to be created from the original source to process it again.

@HowItWorks
1

Creating a Stream

list.stream() produces a Stream view over a collection's elements, ready for a processing pipeline.

2

Intermediate operations

filter, map, sorted, and similar each return another Stream, and are lazy, running nothing yet on their own.

3

Terminal operations

collect, count, forEach, and similar actually trigger the whole chained pipeline to run, all at once.

4

Streams aren't data structures

A Stream is a view over some other source, computed on demand, storing no elements of its own.

5

Streams are single-use

Once a terminal operation consumes a Stream, that specific Stream object is done; a fresh one must be created to process the source again.

@Example
import java.util.List;
import java.util.stream.Collectors;
 
public class StreamDemo {
public static void main(String[] args) {
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
List<Integer> evenSquares = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(evenSquares);
}
}
Output
[4, 16]
1numbers.stream() creates a Stream view over the original list, with no processing done yet.
2filter(n -> n % 2 == 0) is intermediate and lazy, just describing which elements should eventually pass through, not running yet.
3map(n -> n * n) is also intermediate and lazy, describing a transformation to apply to whatever survives the filter.
4collect(Collectors.toList()) is the terminal operation that finally triggers the whole pipeline, producing [4, 16] from the even numbers 2 and 4, each squared.
@UseCases
Filtering, transforming, and collecting a collection's elements in one readable, chained pipeline
Replacing a manual loop that builds up a new filtered or transformed list
Counting, summing, or finding the maximum element in a collection without a hand-written loop
Understanding why a Stream can only be consumed by a terminal operation once

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.