0% found this document useful (0 votes)
35 views2 pages

Java Stream API Overview and Tips

This cheat sheet provides a concise overview of the Java Stream API, including methods for stream creation, intermediate operations for transforming data, terminal operations for obtaining results, and collectors for data aggregation. Key operations such as filter, map, collect, and various collectors are highlighted with examples. Additionally, a practical example demonstrates how to use these methods in a single stream operation.

Uploaded by

saswatagdsc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Java Stream API Overview and Tips

This cheat sheet provides a concise overview of the Java Stream API, including methods for stream creation, intermediate operations for transforming data, terminal operations for obtaining results, and collectors for data aggregation. Key operations such as filter, map, collect, and various collectors are highlighted with examples. Additionally, a practical example demonstrates how to use these methods in a single stream operation.

Uploaded by

saswatagdsc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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!

You might also like