Java & Streams
Updated for 2026

Java Streams API Cheatsheet 2026

Quick reference guide for Java Stream pipelines, intermediate operations (filter, map, flatMap), terminal operations, and collector reduction patterns.

Creation

Stream<String> stream = list.stream();
Create a sequential stream from an existing collection.

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

Console / Terminal
// Processed items on the stream pipeline successfully
Stream<Integer> stream = Stream.of(1, 2, 3);
Create a sequential ordered stream from discrete values.

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<Integer> stream = Stream.of(1, 2, 3);

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
int[] array = {1, 2}; IntStream stream = Arrays.stream(array);
Create a primitive specialization IntStream from an array.

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

int[] array = {1, 2}; IntStream stream = Arrays.stream(array);

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully

Intermediate

stream.filter(x -> x > 10)
Filter elements matching a given boolean predicate.

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

Console / Terminal
// Processed items on the stream pipeline successfully
stream.map(String::toUpperCase)
Transform each element by applying a mapper function.

When 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

Console / Terminal
// Processed items on the stream pipeline successfully
stream.flatMap(List::stream)
Flatten a stream of nested collections into a single flat stream.

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.flatMap(List::stream)

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
stream.sorted()
Sort elements in natural ascending order.

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.sorted()

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
stream.distinct()
Remove duplicate elements based on equals() logic.

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.distinct()

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
stream.limit(5)
Truncate stream size to contain at most 5 elements.

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.limit(5)

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully

Terminal

stream.forEach(System.out::println)
Iterate and perform an action on each element of the stream (Terminal).

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

Console / Terminal
// Processed items on the stream pipeline successfully
List<String> out = stream.collect(Collectors.toList());
Accumulate elements into a new ArrayList container (Terminal).

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

List<String> out = stream.collect(Collectors.toList());

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
Set<String> out = stream.collect(Collectors.toSet());
Accumulate elements into a new unique HashSet container (Terminal).

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

Set<String> out = stream.collect(Collectors.toSet());

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
String combined = stream.collect(Collectors.joining(", "));
Join stream string elements together with a delimiter (Terminal).

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

String combined = stream.collect(Collectors.joining(", "));

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
Optional<Integer> max = stream.max(Integer::compare);
Retrieve the maximum element based on comparator (Terminal).

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

Optional<Integer> max = stream.max(Integer::compare);

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully
int sum = stream.reduce(0, (a, b) -> a + b);
Reduce stream elements to a single value using an accumulator (Terminal).

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

int sum = stream.reduce(0, (a, b) -> a + b);

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully

Collectors

Map<String, List<User>> groups = stream.collect(Collectors.groupingBy(User::getRole));
Group stream elements by a classifier key into a Map (Collectors).

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

Console / Terminal
// Processed items on the stream pipeline successfully
Map<Boolean, List<User>> split = stream.collect(Collectors.partitioningBy(User::isActive));
Partition elements into active/inactive lists based on a predicate (Collectors).

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<Boolean, List<User>> split = stream.collect(Collectors.partitioningBy(User::isActive));

Output Example

Console / Terminal
// Processed items on the stream pipeline successfully

Advanced

stream.parallel()
Convert sequential stream to a parallel stream for multi-threaded processing.

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

Console / Terminal
// Processed items on the stream pipeline successfully

Java 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

Error

IllegalStateException: stream has already been operated upon or closed

Solution

Streams cannot be reused once a terminal operation has executed. Create a new stream pipeline for each subsequent evaluation.

Error

NullPointerException in Collect

Solution

Some collectors (like Collectors.toMap) throw NullPointerException if any values are null. Filter out null values using .filter(Objects::nonNull) prior to collecting.

Error

Infinite Stream Out-of-Memory

Solution

Using infinite generators like Stream.iterate without applying a .limit() boundary first. Always cap infinite stream sequences.

Error

Parallel Stream Performance Degradation

Solution

Running operations that rely on element order (like limit, findFirst) inside parallel streams, forcing thread coordination. Keep parallel flows unordered.

Error

ConcurrentModificationException on Source

Solution

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.