Collectors.LongStatistics Min/Max Values

Paul Sandoz paul.sandoz at oracle.com
Fri Mar 1 02:35:28 PST 2013


Fixed in the tip:

http://hg.openjdk.java.net/lambda/lambda/jdk/rev/e4c9aab508b1

Paul.

On Mar 1, 2013, at 10:23 AM, Paul Sandoz <Paul.Sandoz at oracle.com> wrote:

> 
> On Feb 28, 2013, at 11:49 PM, Niels Nonsense <niels.nwx at gmail.com> wrote:
> 
>> The accept method of LongStatistics should be rewritten as:
>> @Override
>> public void accept(long value) {
>>   sum += value;
>>   min = count == 0 ? value : Math.min(min, value);
>>   max = Math.max(max, value);
>>   ++count;
>> }
>> Similar for combine and the other primitives.
>> 
> 
> A simpler approach is to initialize min to Long.MAX_VALUE and max to Long.MIN_VALUE and the min/max methods should return OptionalLong:
> 
>        public OptionalLong getMin() {
>            return count > 0 ? OptionalLong.of(min) : OptionalLong.empty();
>        }
> 
> to be consistent with the stream min/max methods.
> 
> For DoubleStatistics i think the initial min and max values can be Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY respectively. We should also think about what it means if a NaN is present. We could state the behaviour uses the same rules as Math.min/max, which means if a Nan is present then NaN will be returned.
> 
> Paul.
> 
>> 2013/2/28 Robert J. Saulnier <robert.j.saulnier at gmail.com>
>> 
>>> I'm guessing this was already discussed.
>>> 
>>> Why does the following print "min=0" instead of "min=1"?
>>> 
>>> List<Long> nums = Arrays.asList(1L, 2L, 3L);
>>> 
>>> Collectors.LongStatistics stats =
>>> nums.stream().collect(Collectors.toLongStatistics());
>>> 
>>> System.out.println("min=" + stats.getMin());
>>> 
>>> Same issue with max if all the numbers are negative.
>>> 
>>> 
>> 
> 
> 



More information about the lambda-dev mailing list