New constructor of XXXSummaryStatistics in java-10

I see that java-10 adds a constructor for IntSummaryStatistics ( LongSummaryStatistics and DoubleSummaryStatistics ), which takes 4 parameters count , min , max and sum .

I understand why the no-args constructor exists, so it will be used in abbreviation, for example:

  ..stream().collect(Collectors.summarizingInt(Class::someFunction)) 

This makes sense, but why add a constructor with 4 parameters? (I made an assumption in my own answer, but if it werenโ€™t, I would gladly cancel it.)

+5
source share
3 answers

Note that although this optimization is not performed today in the reference implementation, for an operation such as IntStream.rangeClosed(from, to).summaryStatistics() , it is not necessary to actually IntStream.rangeClosed(from, to).summaryStatistics() over all the values.

It can simply return new IntSummaryStatistics((long)to-from+1, from, to, ((long)from+to)*((long)to-from+1)/2) .

As already mentioned, this optimization does not occur today, but it is an example that sometimes there are ways to calculate such statistics without having to repeat each value, so it was a significant limitation that the only way to populate xxxSummaryStatistics was to acceptโ€‹ separate values โ€‹โ€‹(and combine , but for this an existing instance of statistics is required, which must be filled in somehow).

+3
source

I tried to search for sources if these constructors are used somewhere, and I could not.

Therefore, I only thought that it was used to create such an object manually. Suppose we have a method that calculates all these averages, min, max, count, and instead of returning an array/List of 4 parameters, you can return IntSummaryStatistics , previously this was not possible. So, I think it just extends the API without (for now?) Any internal uses.


From relative CSR exactly: -

Problem : *SummaryStatistics cannot be restored from the recorded values. For example, be "cloned" or transmitted in sequential form and restored.

Solution : Add constructors to *SummaryStatistics to accept the pre-written state.

+5
source

In addition to the answer to the use (necessity) of such a constructor, in order to be concerned about the correct implementation of the API, you really need to take into account the listened API Note that creating such instances is under certain well-considered considerations like (emphasis mine) -

If count is zero, the remaining arguments are ignored and an empty instance is created.

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. Conditions necessary agreed arguments :

  • count> = 0
  • min <= max

But then, since this does not cover all types of checks on the combinations of count , sum , max and min values โ€‹โ€‹that the user can insert, there is this statement (which I found while playing with the constructor)

*SummaryStatistics argument means that the resulting set of recorded values โ€‹โ€‹received from the *SummaryStatistics instance source cannot be a legitimate set of arguments for this constructor due to arithmetic overflow of the reference source of values . consistent arguments are not enough to prevent the creation of an internal inconsistent instance. An example of such a state would be an instance (IntSummaryStatistics) with: count = 2, min = 1, max = 2, and sum = 0.

and add to it such incorrectly created instances when combine d with another *SummaryStatistic can lead to an illegal collection of arguments further.

+2
source

Source: https://habr.com/ru/post/1275709/


All Articles