Sum up ArrayList via Java Stream [duplicate]

The mapToDouble call is not useless: it performs an implicit unboxing. Actually it’s the same as

double totalevent = myList.stream().mapToDouble(f -> f.doubleValue()).sum();

Or

double totalevent = myList.stream().mapToDouble(Double::doubleValue).sum();

Alternatively you can use summingDouble collector, but it’s not a big difference:

double totalevent = myList.stream().collect(summingDouble(f -> f));

In my StreamEx library you can construct a DoubleStream directly from Collection<Double>:

double totalevent = DoubleStreamEx.of(myList).sum();

However internally it also uses mapToDouble.

Leave a Comment