September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's assume that we want to sum all the weights of melons. We did this in the Sum, max, and min in a stream section via primitive specializations of Stream. Now, let's do it via the summingInt(ToIntFunction<? super T> mapper) collector:
int sumWeightsGrams = melons.stream() .collect(Collectors.summingInt(Melon::getWeight));
So, Collectors.summingInt() is a factory method that takes a function that's capable of mapping an object into an int that has to be summed as an argument. A collector is returned that performs the summarization via the collect() method. The following diagram depicts how summingInt() works:

While traversing the ...