Campbell Ritchie wrote:Please explain more; I can't at the moment see what the problem is: sorry.

Well, let me cite the book, p. 220/221:

Summarizing statistics

You've learned enough to be able to get the maximum value from a stream of int primitives. If the stream is empty, we want to throw an exception.

This should be old hat by now. We got an OptionalInt because we have an IntStream. If the optional contains a value, we return it. Otherwise, we throw a new RuntimeException.

Now we want to change the method to take an IntStream and return a range. The range is the minimum value subtracted from the maximum value. Uh-oh. Both min() and max() are terminal operations, which means that they use up the stream when they are run. We can't run two terminal operations against the same stream. Luckily, this is a common problem and the primitive streams solve it for us with summary statistics. Statistic is just a big word for a number that was calculated from data.

Here we asked Java to perform many calculations about the stream. Summary statistics include the following:

• Smallest number (minimum): getMin()

• Largest number (maximum): getMax()

• Average: getAverage()

• Sum: getSum()

• Number of values: getCount()

**If the stream were empty, we'd have a count and sum of zero. The other methods would return an empty optional.**

I'm questioning

__that last sentence__, because it looks like getMin(), getMax() and getAverage() methods of IntSummaryStatistics would return empty optional. But that's not the case. Here are definitions of IntSummaryStatistics methods:

As you can see, all methods of IntSummaryStatics (same as DoubleSummaryStatistics or LongSummaryStatistics) return primitives. The last sentence would be valid, if we were talking about IntStream (DoubleStream, LongStream) methods (see method definitions below ↓), which return Optionals for methods min(), max() and average().

It's possible, that I'm not reading this correctly, but the last sentence is for me in the context of summary statistics objects, so even when IntSummaryStatistics is obtained from an empty stream, its methods should return primitives instead of their wrappers.