Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XXXSummaryStatistics new constructor in java-10

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.)

like image 913
Eugene Avatar asked Mar 05 '18 08:03

Eugene


2 Answers

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.

like image 57
Eugene Avatar answered Sep 20 '22 15:09

Eugene


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 return
new 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).

like image 41
Holger Avatar answered Sep 20 '22 15:09

Holger