Collectors: Aggregation Operations
Explore Java 8 Stream API's Collectors class to perform aggregation operations like counting elements, summing and averaging numeric values, finding minimum and maximum elements, and summarizing statistics from data streams. This lesson helps you apply these methods effectively on collections.
In this lesson, we will look at some of the methods of the Collectors class that help us aggregate the data in streams, e.g., sum, average, etc.
1) counting()
This function returns a Collector that counts the number of the input elements.
Suppose we have a list of employees, and we need the count of employees with an age more than 30.
In this case, we can use the counting() method as shown below.
2) Collectors.summingInt(ToIntFunction<? super T> mapper)
This method returns a ...