7

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

Naman
  • 27,789
  • 26
  • 218
  • 353
Eugene
  • 117,005
  • 15
  • 201
  • 306

3 Answers3

5

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
Eugene
  • 117,005
  • 15
  • 201
  • 306
3

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

Holger
  • 285,553
  • 42
  • 434
  • 765
2

Other than the answer for the usage(need) of such a constructor, to be concerned about the correctness of the API implementation, one should really take into consideration of the called out loud API Note such that creation of such instances are under certain thoughtful considerations as(emphasis mine) -

If the count is zero then the remaining arguments are ignored and an empty instance is constructed.

Example -

var intsummstats = new IntSummaryStatistics();
// creates the following stat
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}

// and the  following results into a similar stat as well
var anotherintsummstats = new IntSummaryStatistics(0, 12, 100, 1000);
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}

If the arguments are inconsistent then an IllegalArgumentException is thrown. The necessary consistent argument conditions are:

  • count >= 0
  • min <= max

But then since this doesn't cover all types of checks over the count, sum, max and min combination of the values the user can put in, there is this statement(which I found while playing around with the constructor)

The enforcement of argument correctness means that the retrieved set of recorded values obtained from an *SummaryStatistics source instance may not be a legal set of arguments for this constructor due to arithmetic overflow of the source's recorded count of values. The consistent argument conditions are not sufficient to prevent the creation of an internally inconsistent instance. An example of such a state would be an (IntSummaryStatistics) instance with: count = 2, min = 1, max = 2, and sum = 0.

and to add to it, such incorrectly created instances when combined with other *SummaryStatistic could result into an illegal set of arguments further.

Naman
  • 27,789
  • 26
  • 218
  • 353