Lambda AttributeValue java流上的summingDouble
我有一个字符串、AttributeValue的映射列表,我想使用Lambda AttributeValue java流上的summingDouble,lambda,java-8,java-stream,reduce,entity-attribute-value,Lambda,Java 8,Java Stream,Reduce,Entity Attribute Value,我有一个字符串、AttributeValue的映射列表,我想使用summingDouble对键进行分组,并分别找到每个键的和 比如说, List<Map<String, AttributeValue>> ls = new ArrayList<Map<String, AttributeValue>>(); Map<String,AttributeValue> map = new HashMap<String,Att
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);
其中,key1是key1的值之和
尝试如下所示,但本例中的getValue是AttributeValue,因此不起作用
Map<String, Double> result = ls.stream().flatMap(m -> m.entrySet().stream()).collect(groupingBy(Map.Entry::getKey, summingDouble(Map.Entry::getValue)));
Map result=ls.stream().flatMap(m->m.entrySet().stream()).collect(groupingBy(Map.Entry::getKey,summingDouble(Map.Entry::getValue));
您的分组方式应该是这样的:
groupingBy(
Map.Entry::getKey,
summingDouble(e -> Double.valueOf(e.getValue().getN())))
如何从
AttributeValue
检索该双精度值?有什么办法吗?@Eugene可能只是getN
?@Eugene我只是在寻找答案。。需要帮助才能从AttributeValue获取双精度。。为了提取存在的双精度值..@Federicoperaltachaffner我猜。。。所以它应该是entry->Double.valueOf(entry.getValue().getN())
Map map1=ls.stream().flatMap(m->m.entrySet().stream()).collect(groupingBy(Map.entry::getKey,summingDouble)(e->Double.valueOf(e.getValue().getN()))
尝试了上述操作,但返回{key1=2000.0,key2=2000.0,key3=2000.0}
@user1712810您期望什么?看看您如何放置ls.add(map)
,然后覆盖同一map
中的值,然后再次执行ls.add(map)
。您可能需要key3
和key1
groupingBy(
Map.Entry::getKey,
summingDouble(e -> Double.valueOf(e.getValue().getN())))