Complete Java Streams cheat sheet — intermediate and terminal operations, collectors, parallel streams with examples.
| Method | Example | Description |
|---|---|---|
| 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) |
| Operation | Signature | Description |
|---|---|---|
| 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 |
| Operation | Returns | Description |
|---|---|---|
| collect() | R | Collect into collection/map/string |
| forEach() | void | Apply action to each element |
| count() | long | Count elements |
| findFirst() | Optional | First element |
| findAny() | Optional | Any element (faster in parallel) |
| anyMatch() | boolean | Any element matches predicate |
| allMatch() | boolean | All elements match predicate |
| noneMatch() | boolean | No elements match predicate |
| min() / max() | Optional | Min/max by comparator |
| reduce() | Optional | Fold elements into single value |
| toArray() | Object[] | Collect into array |
| Collector | Example | Result |
|---|---|---|
| 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 |