Home Cheat Sheets Java Streams API Cheat Sheet
📋 CHEAT SHEET

Java Streams API Cheat Sheet

Complete Java Streams cheat sheet — intermediate and terminal operations, collectors, parallel streams with examples.

Creating Streams

MethodExampleDescription
Collection.stream()list.stream()Sequential stream from collection
Collection.parallelStream()list.parallelStream()Parallel stream from collection
Stream.of()Stream.of(1, 2, 3)Stream from values
Arrays.stream()Arrays.stream(arr)Stream from array
Stream.iterate()Stream.iterate(0, n->n+1)Infinite sequential stream
Stream.generate()Stream.generate(Math::random)Infinite unordered stream
IntStream.range()IntStream.range(0, 10)Range of ints (exclusive end)
IntStream.rangeClosed()IntStream.rangeClosed(1, 10)Range of ints (inclusive end)

Intermediate Operations (lazy)

OperationSignatureDescription
filter()filter(Predicate)Keep elements matching condition
map()map(Function)Transform each element
flatMap()flatMap(Function)Flatten nested streams
distinct()distinct()Remove duplicates
sorted()sorted() / sorted(Comparator)Sort elements
limit()limit(long n)Take first n elements
skip()skip(long n)Skip first n elements
peek()peek(Consumer)Debug — apply action, pass through
mapToInt()mapToInt(ToIntFunction)Convert to IntStream

Terminal Operations (eager)

OperationReturnsDescription
collect()RCollect into collection/map/string
forEach()voidApply action to each element
count()longCount elements
findFirst()OptionalFirst element
findAny()OptionalAny element (faster in parallel)
anyMatch()booleanAny element matches predicate
allMatch()booleanAll elements match predicate
noneMatch()booleanNo elements match predicate
min() / max()OptionalMin/max by comparator
reduce()Optional / TFold elements into single value
toArray()Object[]Collect into array

Common Collectors

CollectorExampleResult
toList().collect(Collectors.toList())List
toSet().collect(Collectors.toSet())Set
toMap().collect(Collectors.toMap(k, v))Map
joining().collect(Collectors.joining(", "))String
groupingBy().collect(Collectors.groupingBy(f))Map>
partitioningBy().collect(Collectors.partitioningBy(p))Map>
counting().collect(Collectors.counting())Long
summarizingInt().collect(Collectors.summarizingInt(f))IntSummaryStatistics

Parallel Stream Gotchas

GotchaProblemFix
Not always fasterThread overhead dominates on small collections (<10k elements)Benchmark with JMH before using parallel()
Shared mutable stateRace conditions on non-thread-safe accumulatorsUse thread-safe collectors — never mutate external list
forEach order not guaranteedParallel forEach processes in arbitrary orderUse forEachOrdered() if order matters (but kills parallelism)
Custom thread poolparallel() uses ForkJoinPool.commonPool() — shared with rest of appWrap in ForkJoinPool: pool.submit(() -> list.parallelStream()...).get()
stateful intermediate opssorted(), distinct() on parallel streams force sequential mergeAvoid stateful ops in parallel — defeats the purpose
findFirst vs findAnyfindFirst preserves encounter order (slower in parallel)Use findAny() when you only need any match — faster
collect() thread safetyCollectors.toList() is safe; manual ArrayList::add is notAlways use built-in Collectors — never collect into shared mutable container
More Cheat Sheets
Java Collections Cheat SheetPython Built-in Functions Cheat SheetSQL Joins Cheat SheetDocker Commands Cheat SheetJVM Memory Model DiagramHow HashMap Works InternallyMicroservices Cheat SheetPandas Cheat SheetData Structures & Algorithms Cheat Sheet