Java Programming Tutorial 24: Streams (Part 2)311
In the previous tutorial, we introduced the concept of streams and how they can be used to process data in a more efficient and declarative way. In this tutorial, we will continue our exploration of streams by covering some of the more advanced features, including stream reduction, grouping, and partitioning.
Stream Reduction
Stream reduction is the process of combining all the elements of a stream into a single value. This can be done using the reduce() method, which takes a binary operator as an argument and applies it to each element of the stream, starting with an initial value.
Stream numbers = (1, 2, 3, 4, 5);
int sum = (0, (a, b) -> a + b);
(sum); // prints 15
In this example, we are using the reduce() method to sum all the elements of the numbers stream. The initial value of 0 is used as the starting point for the reduction, and the binary operator (a, b) -> a + b adds the current element (b) to the accumulator (a).
Stream Grouping
Stream grouping is the process of dividing a stream into groups based on a common property. This can be done using the groupBy() method, which takes a classifier function as an argument and returns a map from the classifier values to the corresponding elements.
Stream students = (
new Student("Alice", "Math"),
new Student("Bob", "Science"),
new Student("Carol", "Math"),
new Student("Dave", "Science")
);
Map studentsByMajor = (Student::getMajor);
(studentsByMajor); // prints {Math=[Alice, Carol], Science=[Bob, Dave]}
In this example, we are using the groupBy() method to group the students stream by their major. The classifier function Student::getMajor returns the major of each student, and the resulting map contains two keys: "Math" and "Science". The values associated with each key are lists of students with that major.
Stream Partitioning
Stream partitioning is the process of dividing a stream into two groups based on a predicate. This can be done using the partitionBy() method, which takes a predicate as an argument and returns a pair of streams: one containing the elements that satisfy the predicate, and the other containing the elements that do not.
Stream numbers = (1, 2, 3, 4, 5);
Pair partitioned = (n -> n % 2 == 0);
(()); // prints [2, 4]
(()); // prints [1, 3, 5]
In this example, we are using the partitionBy() method to partition the numbers stream into two groups: one containing the even numbers, and the other containing the odd numbers. The predicate n -> n % 2 == 0 tests whether a number is even, and the resulting pair of streams contains the even numbers in the first stream and the odd numbers in the second stream.
Conclusion
In this tutorial, we have covered some of the more advanced features of streams, including stream reduction, grouping, and partitioning. These features can be used to perform a wide variety of data processing tasks in a more efficient and declarative way.
In the next tutorial, we will take a look at some of the more advanced stream operations, such as flatMap() and peek().
2025-01-20
A Comprehensive Guide to the Bavarian Dialect of Munich
https://zeidei.com/lifestyle/46808.html
How to Live Stream Music on Kurzgesagt - A Comprehensive Guide
https://zeidei.com/arts-creativity/46807.html
The Ultimate Guide to Pivot Tables: A Step-by-Step Tutorial
https://zeidei.com/technology/46806.html
Locust Flower Delicacy: A Culinary Exploration
https://zeidei.com/lifestyle/46805.html
Guide to Top Healthcare Stocks to Watch
https://zeidei.com/health-wellness/46804.html
Hot
A Beginner‘s Guide to Building an AI Model
https://zeidei.com/technology/1090.html
DIY Phone Case: A Step-by-Step Guide to Personalizing Your Device
https://zeidei.com/technology/1975.html
Odoo Development Tutorial: A Comprehensive Guide for Beginners
https://zeidei.com/technology/2643.html
Android Development Video Tutorial
https://zeidei.com/technology/1116.html
Database Development Tutorial: A Comprehensive Guide for Beginners
https://zeidei.com/technology/1001.html