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.

Read Full Tutorial →

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
More Cheat Sheets
Java Collections Cheat SheetPython Built-in Functions Cheat SheetSQL Joins Cheat SheetDocker Commands Cheat SheetJVM Memory Model DiagramHow HashMap Works InternallyMicroservices Architecture DiagramPandas Cheat Sheet