sum() and average()

If you need to calculate a sum or an average of the values of numeric stream elements, the only requirement for the stream is that it should not be infinite. Otherwise, the calculation never finishes:

int sum = IntStream.empty().sum();
System.out.println(sum); //prints: 0

sum = IntStream.range(1, 3).sum();
System.out.println(sum); //prints: 3

double
av = IntStream.empty().average().orElse(0);
System.out.println(av); //prints: 0.0

av = IntStream.range(1, 3).average().orElse(0);
System.out.println(av); //prints: 1.5

long
suml = LongStream.range(1, 3).sum();
System.out.println(suml); //prints: 3

double
avl = LongStream.range(1, 3).average().orElse(0);
System.out.println(avl); //prints: 1.5

double
sumd = DoubleStream.of(1, 2).sum();
System.out.println(sumd); //prints: 3.0

double
avd = DoubleStream.of(1, 2).average().orElse(0);
System.out.println(avd); //prints: 1.5

As you can see, using these operations on an empty stream is not a problem.