Java Streams API Cheatsheet 2026
Quick reference guide for Java Stream pipelines, intermediate operations (filter, map, flatMap), terminal operations, and collector reduction patterns.
Creation
When to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
Stream<String> stream = list.stream();Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
Stream<Integer> stream = Stream.of(1, 2, 3);Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
int[] array = {1, 2}; IntStream stream = Arrays.stream(array);Output Example
// Processed items on the stream pipeline successfullyIntermediate
When to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.filter(x -> x > 10)Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When mapping, altering, or projecting elements within a stream from one type to another.
Common Mistakes
Passing a mapping function that can throw checked exceptions directly without wrapping them.
Shortcut / Pro-Tip
Use method references like User::getName instead of full lambdas for clean code readability.Example
stream.map(String::toUpperCase)Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.flatMap(List::stream)Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.sorted()Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.distinct()Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.limit(5)Output Example
// Processed items on the stream pipeline successfullyTerminal
When to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.forEach(System.out::println)Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When terminating a stream and packaging results back into solid structures like Lists, Sets, or Maps.
Common Mistakes
Modifying external shared state variables from inside a parallel stream collect operation.
Shortcut / Pro-Tip
Use Collectors.toUnmodifiableList() for read-only safety on output results.Example
List<String> out = stream.collect(Collectors.toList());Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When terminating a stream and packaging results back into solid structures like Lists, Sets, or Maps.
Common Mistakes
Modifying external shared state variables from inside a parallel stream collect operation.
Shortcut / Pro-Tip
Use Collectors.toUnmodifiableList() for read-only safety on output results.Example
Set<String> out = stream.collect(Collectors.toSet());Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When terminating a stream and packaging results back into solid structures like Lists, Sets, or Maps.
Common Mistakes
Modifying external shared state variables from inside a parallel stream collect operation.
Shortcut / Pro-Tip
Use Collectors.toUnmodifiableList() for read-only safety on output results.Example
String combined = stream.collect(Collectors.joining(", "));Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
Optional<Integer> max = stream.max(Integer::compare);Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
int sum = stream.reduce(0, (a, b) -> a + b);Output Example
// Processed items on the stream pipeline successfullyCollectors
When to Use
When terminating a stream and packaging results back into solid structures like Lists, Sets, or Maps.
Common Mistakes
Modifying external shared state variables from inside a parallel stream collect operation.
Shortcut / Pro-Tip
Use Collectors.toUnmodifiableList() for read-only safety on output results.Example
Map<String, List<User>> groups = stream.collect(Collectors.groupingBy(User::getRole));Output Example
// Processed items on the stream pipeline successfullyWhen to Use
When terminating a stream and packaging results back into solid structures like Lists, Sets, or Maps.
Common Mistakes
Modifying external shared state variables from inside a parallel stream collect operation.
Shortcut / Pro-Tip
Use Collectors.toUnmodifiableList() for read-only safety on output results.Example
Map<Boolean, List<User>> split = stream.collect(Collectors.partitioningBy(User::isActive));Output Example
// Processed items on the stream pipeline successfullyAdvanced
When to Use
When transforming, filtering, or aggregating Java collection pipelines in a functional style.
Common Mistakes
Trying to reuse a closed stream; streams can only be traversed once, otherwise throwing an IllegalStateException.
Shortcut / Pro-Tip
Use primitive streams (IntStream, LongStream) instead of Stream<Integer> to avoid costly auto-boxing overhead.Example
stream.parallel()Output Example
// Processed items on the stream pipeline successfullyJava Streams Best Practices
1Prefer Lazy Terminal operations
Streams are lazy; intermediate operations are not executed until a terminal operation (like collect, forEach, or reduce) is invoked.
2Avoid Side Effects inside Streams
Keep stream operations stateless and pure. Avoid mutating shared state or external variables from lambdas.
3Use Parallel Streams Wisely
Only use parallelStream() for very large datasets and computationally intensive tasks where thread management overhead is offset by gain.
4Use Primitive Streams to Avoid Box Overhead
Use IntStream, LongStream, and DoubleStream instead of Stream<Integer> or Stream<Double> to skip unnecessary auto-boxing.
5Ensure Closed Resources with Try-with-resources
When working with Streams backed by IO resources (like Files.lines), use a try-with-resources block to guarantee close handling.
Common Java Streams Errors & Solutions
IllegalStateException: stream has already been operated upon or closed
Streams cannot be reused once a terminal operation has executed. Create a new stream pipeline for each subsequent evaluation.
NullPointerException in Collect
Some collectors (like Collectors.toMap) throw NullPointerException if any values are null. Filter out null values using .filter(Objects::nonNull) prior to collecting.
Infinite Stream Out-of-Memory
Using infinite generators like Stream.iterate without applying a .limit() boundary first. Always cap infinite stream sequences.
Parallel Stream Performance Degradation
Running operations that rely on element order (like limit, findFirst) inside parallel streams, forcing thread coordination. Keep parallel flows unordered.
ConcurrentModificationException on Source
Modifying the backing collection while streaming over it. Always collect elements into a new structure before modifying the source.
Common Java Streams Interview Questions
Q1What is a Java Stream and how does it differ from a Collection?
A Stream is a sequence of elements supporting sequential and parallel aggregate operations. Unlike Collections, Streams do not store elements; they are pipeline processors that do not modify the backing source.
Q2What is the difference between intermediate and terminal operations?
Intermediate operations (like filter, map) are lazy and return a new Stream, enabling pipelining. Terminal operations (like collect, findFirst) close the Stream, initiate processing, and return a non-stream result.
Q3Why should you be careful when using parallel streams?
Parallel streams utilize the shared ForkJoinPool.commonPool(). High-latency or blocking tasks in parallel streams can choke the pool and slow down other unrelated processes across the JVM.
Q4What is the difference between map() and flatMap()?
map() transforms each element into a single new element (1-to-1). flatMap() transforms each element into a stream of elements and flattens all nested streams into a single output stream (1-to-many).
Q5What are some common short-circuiting terminal operations?
Short-circuiting terminal operations (like findFirst, findAny, anyMatch, allMatch, noneMatch) do not need to process all elements in the stream to produce a final result.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com