Java Stream API Cheat Sheet
1. Stream Creation
[Link](arr) // From array
[Link]() // From List
[Link](1, 2, 3) // Direct values
[Link](1, 5) // 1 to 4
2. Intermediate Operations (Transforming Data)
filter() - Keep elements that match condition ex: .filter(n -> n % 2 == 0)
map() - Convert each element ex: .map(n -> n * 2)
sorted() - Sort elements ex: .sorted()
distinct() - Remove duplicates ex: .distinct()
limit(n) - Keep only first n elements ex: .limit(3)
skip(n) - Skip first n elements ex: .skip(2)
peek() - Inspect stream steps ex: .peek([Link]::println)
3. Terminal Operations (Get Results)
collect() - Gather into List, Set, Map ex: .collect([Link]())
forEach() - Perform action on each element ex: .forEach([Link]::println)
count() - Number of elements ex: .count()
toArray() - Convert to array ex: .toArray()
reduce() - Accumulate into a single result ex: .reduce(0, Integer::sum)
anyMatch() - True if any match condition ex: .anyMatch(n -> n > 10)
allMatch() - True if all match condition ex: .allMatch(n -> n > 0)
noneMatch() - True if none match condition ex: .noneMatch(n -> n < 0)
findFirst() - Get the first element (Optional) ex: .findFirst()
4. Collectors (for collect())
[Link]() - Convert to List
[Link]() - Convert to Set
[Link](", ") - Join strings with separator
[Link]() - Group by key
[Link]() - Convert to map
[Link]() - Count elements
[Link]() - Sum of integers
5. Example: All-in-One
List<Integer> result = [Link]()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.sorted()
.collect([Link]());
Tip: Use stream().peek() for debugging intermediate steps!