0% found this document useful (0 votes)
4 views4 pages

Mastering Java Stream API Operations

The document provides a quick guide to Java Streams, detailing various Stream API operations such as filtering, mapping, reducing, grouping, sorting, and more. Each operation is illustrated with examples and outputs, demonstrating how to manipulate collections effectively. Key functionalities include removing duplicates, limiting or skipping elements, flattening nested collections, and joining strings.

Uploaded by

Abhinay Soni
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)
4 views4 pages

Mastering Java Stream API Operations

The document provides a quick guide to Java Streams, detailing various Stream API operations such as filtering, mapping, reducing, grouping, sorting, and more. Each operation is illustrated with examples and outputs, demonstrating how to manipulate collections effectively. Key functionalities include removing duplicates, limiting or skipping elements, flattening nested collections, and joining strings.

Uploaded by

Abhinay Soni
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 Streams

Like a Pro
A Quick Guide to
Stream API
Operations

Khaled Bakarman
Java Streams Like a Pro

1. Filter and Collect


Use filter to select elements based on a condition.

Example:
List<String> names = [Link]("Alice", "Bob", "Andrew", "Brian");
List<String> filtered = [Link]()
.filter(name -> [Link]("A"))
.collect([Link]());

Output: ["Alice", "Andrew"]

2. Map and Collect


Transform each element using map.

Example:
List<String> names = [Link]("John", "Doe");
List<Integer> lengths = [Link]()
.map(String::length)
.collect([Link]());

Output: [4, 3]

3. Reduce
Reduce is used to combine elements into a single result.

Example:
List<Integer> numbers = [Link](1, 2, 3, 4);
int sum = [Link]()
.reduce(0, Integer::sum);

Output: 10

4. Grouping By
Group elements by a classifier function.

Example:
List<String> words = [Link]("apple", "banana", "apricot", "blueberry");
Map<Character, List<String>> grouped = [Link]()
.collect([Link](w -> [Link](0)));

Page 1
Java Streams Like a Pro
Output: {a=[apple, apricot], b=[banana, blueberry]}

5. Sorting
Sort elements in natural or custom order.

Example:
List<String> names = [Link]("Charlie", "Alice", "Bob");
List<String> sorted = [Link]()
.sorted()
.collect([Link]());

Output: ["Alice", "Bob", "Charlie"]

6. Distinct
Remove duplicates from the stream.

Example:
List<Integer> nums = [Link](1, 2, 2, 3);
List<Integer> unique = [Link]()
.distinct()
.collect([Link]());

Output: [1, 2, 3]

7. Limit and Skip


Limit or skip elements in a stream.

Example:
List<Integer> limited = [Link](1, n -> n + 1)
.limit(5)
.collect([Link]());

List<Integer> skipped = [Link](1, 2, 3, 4, 5)


.skip(2)
.collect([Link]());

Output (limited): [1, 2, 3, 4, 5]


Output (skipped): [3, 4, 5]

8. FlatMap

Page 2
Java Streams Like a Pro
Flatten nested collections.

Example:
List<List<String>> data = [Link](
[Link]("a", "b"),
[Link]("c", "d")
); List<String> flat =
[Link]()
.flatMap(Collection::stream)
.collect([Link]());

Output: ["a", "b", "c", "d"]

9. Peek
Inspect elements without modifying them.

Example:
List<String> result = [Link]("one", "two", "three")
.peek([Link]::println)
.collect([Link]());

Output: prints 'one', 'two', 'three' during processing

10. Collecting and Joining


Join strings with delimiter.

Example:
List<String> words = [Link]("Java", "Streams");
String joined = [Link]()
.collect([Link](", "));

Output: "Java, Streams"

Page 3

You might also like