Java Streams API Cheat Sheet — Quick Reference with Daily Use Examples
Java Streams API Cheat Sheet — Quick Reference with Daily Use Examples
Introduction
The Java Streams API allows developers to process collections in a functional and elegant way. Whether it’s filtering, transforming, or collecting results, streams can make your code more readable and concise. This cheat sheet is your quick reference for daily use — packed with simple explanations and ready-to-use code examples.
1️⃣ Creating Streams
// From a collection
List<String> list = List.of("a", "b", "c");
Stream<String> stream = list.stream();
// From an array
Stream<Integer> arrayStream = Arrays.stream(new Integer[]{1, 2, 3});
// Using Stream.of()
Stream<String> streamOf = Stream.of("one", "two", "three");
2️⃣ Intermediate Operations
Operation | Example | What it does |
---|---|---|
filter() | stream.filter(x -> x.startsWith("A")) | Filters elements based on a condition |
map() | stream.map(String::toUpperCase) | Transforms each element |
flatMap() | list.stream().flatMap(List::stream) | Flattens nested collections |
distinct() | stream.distinct() | Removes duplicates |
sorted() | stream.sorted() | Sorts elements |
limit() | stream.limit(5) | Limits the number of elements |
skip() | stream.skip(3) | Skips the first 3 elements |
3️⃣ Terminal Operations
Operation | Example | What it does |
---|---|---|
collect() | stream.collect(Collectors.toList()) | Collects elements into a collection |
count() | stream.count() | Counts elements |
forEach() | stream.forEach(System.out::println) | Performs an action on each element |
reduce() | stream.reduce("", String::concat) | Reduces stream to a single value |
anyMatch() | stream.anyMatch(x -> x.length() > 3) | Checks if any match the condition |
allMatch() | stream.allMatch(x -> x.length() > 1) | Checks if all match the condition |
noneMatch() | stream.noneMatch(x -> x.isEmpty()) | Checks if none match the condition |
4️⃣ Collectors Quick Reference
Operation | Example | Purpose |
---|---|---|
To List | collect(Collectors.toList()) | Collect results into a List |
Grouping By | collect(Collectors.groupingBy(Person::getAge)) | Group elements by a property |
Joining Strings | collect(Collectors.joining(", ")) | Join strings with delimiter |
Summing | collect(Collectors.summingInt(Item::getQuantity)) | Sum values of a property |
5️⃣ Common Use Cases
Find duplicates:
List<String> duplicates = list.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()))
.entrySet().stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Count frequency of elements:
Map<String, Long> frequency = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Sort by custom property:
list.stream()
.sorted(Comparator.comparing(String::length))
.forEach(System.out::println);
Group by first letter:
Map<Character, List<String>> grouped = list.stream()
.collect(Collectors.groupingBy(s -> s.charAt(0)));