lambda - AttributeValue java流上的summingDouble
问题描述
我有一个 String、AttributeValue 的 Map 列表,并想使用summingDouble
它来对键进行分组并分别找到每个键的总和。
例如,
List<Map<String, AttributeValue>> ls = new ArrayList<Map<String, AttributeValue>>();
Map<String,AttributeValue> map = new HashMap<String,AttributeValue>();
map.put("key1", new AttributeValue().withN("1000"));
map.put("key2", new AttributeValue().withN("1000"));
ls.add(map);
Map<String,AttributeValue> map1 = new HashMap<String,AttributeValue>();
map1.put("key3", new AttributeValue().withN("1000"));
map1.put("key1", new AttributeValue().withN("2000"));
ls.add(map1);
这应该返回
result = {key1: 3000, key2: 1000, key3: 1000}
其中 key1 是 key1 的值的总和。
尝试如下,但在这种情况下 getValue 是一个 AttributeValue 所以它不起作用。
Map<String, Double> result = ls.stream().flatMap(m -> m.entrySet().stream()).collect(groupingBy(Map.Entry::getKey, summingDouble(Map.Entry::getValue)));
解决方案
看来您的 groupingBy 应该是这样的:
groupingBy(
Map.Entry::getKey,
summingDouble(e -> Double.valueOf(e.getValue().getN())))
推荐阅读
- c++ - 多线程 C++ winsock 套接字中重复和丢失消息的原因?
- reactjs - Prettier 在 setState 中移动 paren,触发无序列。如何改进 setState?
- javascript - 如何调用组件的 onClick 属性进行测试?
- regex - 使用 grep -P 和lookahead/lookbehind 获取模式之间的文本
- javascript - 在 JavaScript 中,我不能为字符串数组创建唯一值,也不能从另一个数组中排除确定的值
- python - 如何修复 /usr/local/bin/virtualenv: /usr/bin/python: 错误的解释器:没有这样的文件或目录?
- java - 如何将部门中的属性设置在特定数字之间?
- html - 选择列表的 Linq SqlExpression 查询未排序
- string - 如何删除字符串的第一个字符?[卢阿]
- xamarin.forms - Xamarin Forms 服务在包为 com.google.android.gm 时获取类 android.widget.EditText 的文本