我需要在 lambda 中执行以下操作,但无法想出单流和减少可以提供帮助的方法:(
我有 Employee ArrayList,其中名称成员变量可以在列表中的对象之间重复。我需要创建映射,其中键是员工姓名,值是对象,其中我们有该员工的 cost1 和 cost2总和。
Employee 类有 -> 字符串名称、整数成本 1、整数成本 2
输出类有 -> Integer cost1, Integer cost2
List <Employee> employeeList = new ArrayList<>();
// data population in inputMap
Map<String, Output> outputMap = new HashMap<>();
for (Employee emp : employeeList)
{
Output op = outputMap.get(emp.getName());
if(op == null){
Output newOp = new Output ();
newOp.setCost1(emp.getCost1())
newOp.setCost2(emp.getCost2())
newOp.put(emp.getName(), newOp);
}else{
int cost1 = op.getCost1() + emp.getCost1();
int cost2 = op.getCost2() + emp.getCost2(); …
Run Code Online (Sandbox Code Playgroud)