I have a series of double values which I want to sum up and get the maximum value.
The DoubleStream.summaryStatistics()
sounds perfect for that.
The getSum()
method has an API note reminding me of what I learned during one of my computer science courses: the stability of the summation problem tends to be better if the values are sorted by their absolute values. However, DoubleStream
does not let me specify the comparator to use, it will just use Double.compareTo
if I call sorted()
on the stream.
Thus I gathered the values into a final Stream.Builder<Double> values = Stream.builder();
and call
values.build()
.sorted(Comparator.comparingDouble(Math::abs))
.mapToDouble(a -> a).summaryStatistics();
Yet, this looks somewhat lengthy and I would have preferred to use the DoubleStream.Builder
instead of the generic builder.
Did I miss something or do I really have to use the boxed version of the stream just to be able to specify the comparator?