I see that java-10
adds a constructor for IntSummaryStatistics
(LongSummaryStatistics
and DoubleSummaryStatistics
) that takes 4 parameters that are count
, min
, max
and sum
.
I understand why the no-args constructor exists, so that it would be used in reduction, like :
..stream().collect(Collectors.summarizingInt(Class::someFunction))
That makes sense, but why is there a need to add the constructor with 4 parameters? (I made an assumption in my own answer, but if that is not the case I would gladly retract it.)
I've tried to search the sources if these constructors are used somewhere and I could not.
So my only thought would be that it is used to constructor such an Object manually. Suppose that we have a method that computes all these average, min, max, count and instead of returning an array/List
of 4 parameters, you could return an IntSummaryStatistics
, previously this was not possible. So, I guess, this just extends the API without having (yet?) any internal usages.
From the relative CSR precisely :-
Problem :
It is not possible to reconstruct *SummaryStatistics
from it's recorded values. For example to be "cloned" or transmitted in a serial form and reconstituted.
Solution :
Add constructors to *SummaryStatistics
accepting pre-recorded state.
Note that while such an optimization does not happen today in the reference implementation, an operation like IntStream.rangeClosed(from, to).summaryStatistics()
does not need to actually iterate over all values.
It could simply returnnew IntSummaryStatistics((long)to-from+1, from, to, ((long)from+to)*((long)to-from+1)/2)
.
As said, this optimization does not happen today, but is an example that sometimes, there are ways to calculate such a statistic without having to iterate every value, so it was a significant limitation that the only way to fill a xxxSummaryStatistics was accept
ing individual values (and combine
, but that requires an already existing statistics instance that must have been filled somehow).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With