sum return type for primitive streams

Georgiy Rakov georgiy.rakov at
Fri Feb 1 06:43:04 PST 2013


IntStream sum method return type is a primitive while the return type of 
min, max, average is Optional...:

         default long sum() {
             return collect(Collectors.intSumAsLong()).sum();

         default OptionalInt min() {
             return reduce(Math::min);

         default OptionalInt max() {
             return reduce(Math::max);

         default OptionalDouble average() {
             return collect(Collectors.intCountAndSumAsLong()).mean();

Could you please tell if it's a bug or expected behavior. If it's 
expected behavior could you please clarify why it is because from the 
first glance OptionalLong should be the return type for sum method.

The same is true for DoubleStream and LongStream.

Thank you, Georgiy.

More information about the lambda-dev mailing list