java - 获取地图中值的总和
问题描述
在对每个值应用操作后,我必须得到地图中值的总和。
我已经这样做了:
Map<Employee, Integer> employeeBudget = committedHoursPerDay.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, o -> o.getKey().getHourlyWage() * o.getValue()));
//Get the total cost for all the employee
int manpowerBudget = employeeBudget.values()
.stream()
.mapToInt(Integer::intValue)
.sum();
此解决方案有效。我认为有更好的方法来解决这个问题,但我无法弄清楚。
解决方案
事实上,我认为你可以只用一个流来做到这一点:
int manpowerBudget = committedHoursPerDay.entrySet().stream()
.mapToInt(kv-> kv.getKey().getHourlyWage() * kv.getValue())
.sum();
推荐阅读
- python - Pytorch:交叉熵的维度是正确的,但对于 MSE 来说却是错误的?
- java - 堆转储的java进程问题的systemd
- linux - 通过内核读取物理地址中的值
- c - CODEVITA“相似字符”如何降低我在c中的时间复杂度?
- python - 如何使用整数除法(//)访问 numpy python 中的中间行和列
- javascript - 显示特定的setTimeout() 处的内容
- compiler-construction - 语法错误的处理如何涉及解析器和词法分析器的使用?
- c# - 如何使用 SqlCommand 搜索具有多个输入的数据
- python - 基于矩阵数据框创建字典
- node.js - 生成 PDF 时未找到车把链接的 CSS