Java Reference
In-Depth Information
Figure 6.2. The aggregation process of the summingInt collector
The Collectors.summingLong and Collectors.summingDouble methods behave exactly the same
way and can be used where the field to be summed is respectively a long or a double.
But there's more to summarization than mere summing; also available is a
Collectors .averagingInt, together with its averagingLong and averagingDouble counterparts, to
calculate the average of the same set of numeric values:
double avgCalories =
menu.stream().collect(averagingInt(Dish::getCalories));
So far, you've seen how to use collectors to count the elements in a stream, find the maximum
and minimum values of a numeric property of those elements, and calculate their sum and
average. Quite often, though, you may want to retrieve two or more of these results, and possibly
you'd like to do it in a single operation. In this case, you can use the collector returned by the
summarizingInt factory method. For example, you can count the elements in the menu and
 
Search WWH ::




Custom Search