How to sum values in a Map with a stream?

JavaJava 8Java StreamGenerics

Java Problem Overview


I want the equivalent of this with a stream:

public static <T extends Number> T getSum(final Map<String, T> data) {
    T sum = 0;
    for (String key: data.keySet())
        sum += data.get(key);
    return sum;
}

This code doesn't actually compile because 0 cannot be assigned to type T, but you get the idea.

Java Solutions


Solution 1 - Java

You can do this:

int sum = data.values().stream().mapToInt(Integer::parseInt).sum();

Solution 2 - Java

Here's another way to do this:

int sum = data.values().stream().reduce(0, Integer::sum);

(For a sum to just int, however, Paul's answer does less boxing and unboxing.)

As for doing this generically, I don't think there's a way that's much more convenient.

We could do something like this:

static <T> T sum(Map<?, T> m, BinaryOperator<T> summer) {
    return m.values().stream().reduce(summer).get();
}

int sum = MyMath.sum(data, Integer::sum);

But you always end up passing the summer. reduce is also problematic because it returns Optional. The above sum method throws an exception for an empty map, but an empty sum should be 0. Of course, we could pass the 0 too:

static <T> T sum(Map<?, T> m, T identity, BinaryOperator<T> summer) {
    return m.values().stream().reduce(identity, summer);
}

int sum = MyMath.sum(data, 0, Integer::sum);

Solution 3 - Java

Integer count = intMap.values().stream().mapToInt(d-> d).sum();

Get the HashMap.values method to Stream and collect mapToIn with lambda expression ending with aggregation sum method.

Solution 4 - Java

You can do it like this:

int creditAmountSum = result.stream().map(e -> e.getCreditAmount()).reduce(0, (x, y) -> x + y);

Solution 5 - Java

For Doubles you can use this simply.

Double sum = myMap.values().stream().mapToDouble(d -> d).sum();

works for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayView Question on Stackoverflow
Solution 1 - JavaPaul BoddingtonView Answer on Stackoverflow
Solution 2 - JavaRadiodefView Answer on Stackoverflow
Solution 3 - Javauser3264615View Answer on Stackoverflow
Solution 4 - JavaSaeed ZarinfamView Answer on Stackoverflow
Solution 5 - JavasachyyView Answer on Stackoverflow