How to sum values in a Map with a stream?(如何将 Map 中的值与流相加?)
问题描述
我想要一个流的等价物:
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;
}
这段代码实际上并没有编译,因为 0 不能分配给类型 T,但你明白了.
This code doesn't actually compile because 0 cannot be assigned to type T, but you get the idea.
推荐答案
这是另一种方法:
int sum = data.values().stream().reduce(0, Integer::sum);
(不过,对于 int 的总和,Paul 的回答减少了装箱和拆箱.)
(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.
我们可以这样做:
static <T> T sum(Map<?, T> m, BinaryOperator<T> summer) {
return m.values().stream().reduce(summer).get();
}
int sum = MyMath.sum(data, Integer::sum);
但你总是会度过夏天.reduce 也有问题,因为它返回 Optional.上面的 sum 方法对空映射抛出异常,但是空的 sum 应该是 0.当然,我们也可以传递 0:
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);
这篇关于如何将 Map 中的值与流相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Map 中的值与流相加?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
