【发布时间】:2015-09-14 18:57:46
【问题描述】:
我有以下类型的Map
public class MapUtils {
private Map<String, Integer> queryCounts = new HashMap<>();
public void averageCounters(){
int totalCounts = queryCounts.values().stream().reduce(0, Integer::sum);
queryCounts = queryCounts.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
(Map.Entry::getValue)/totalCounts
));
}
这不会编译并在此行 (Map.Entry::getValue)/totalCounts 中显示错误。我该如何解决?有没有更好的方法来使用 Java 8 API 获得超过 Map 的平均值?
编辑: 这是更好的方法吗?
queryCounts.entrySet()
.forEach(entry -> queryCounts.put(entry.getKey(),
entry.getValue()/totalCounts));
【问题讨论】:
标签: dictionary java-8 java-stream