使用Stream api为多个变量减少和分配

KRS*_*KRS 3 lambda java-8 java-stream

我需要在 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();
       op.setCost1(cost1);
       op.setCost2(cost2);
       newOp.put(emp.getName(), op);
    }
}
Run Code Online (Sandbox Code Playgroud)

Eug*_*ene 5

我假设你有这样的结构:

static class Output {
    private final int cost1;

    private final int cost2;

    public Output(int cost1, int cost2) {
        this.cost1 = cost1;
        this.cost2 = cost2;
    } 

    @Override
    public String toString() {
        return "cost1 = " + cost1 + " cost2 = " + cost2;
    }
    // ... getter
}  

static class Employee {
    private final String name;

    private final int cost1;

    private final int cost2;

    public Employee(String name, int cost1, int cost2) {
        this.name = name;
        this.cost1 = cost1;
        this.cost2 = cost2;
    }
 // ...getters      
}
Run Code Online (Sandbox Code Playgroud)

然后解决方案是首先分组Employee::getName并减少到Output通过Collectors.reducing

  Map<String, Output> map = Arrays.asList(
                       new Employee("e", 12, 12), 
                       new Employee("f", 13, 13), 
                       new Employee("e", 11, 11))
            .stream()
            .collect(Collectors.groupingBy(Employee::getName,
                    Collectors.reducing(
                            new Output(0, 0),
                            emp -> new Output(emp.getCost1(), emp.getCost2()),
                            (left, right) -> new Output(left.getCost1() + right.getCost1(), left.getCost2() + right.getCost2()))));
    System.out.println(map); // {e=cost1 = 23 cost2 = 23, f=cost1 = 13 cost2 = 13}
Run Code Online (Sandbox Code Playgroud)

  • 看起来像 `toMap` 收集器的任务: `Collectors.toMap(Employee::getName, emp -&gt; new Output(emp.getCost1(), emp.getCost2()), (left, right) -&gt; new Output( left.getCost1() + right.getCost1(), left.getCost2() + right.getCost2()))`,同样的功能,更少的代码,甚至创建更少的`Output`实例...... (3认同)
  • @FedericoPeraltaSchaffner 请提供它,这将是一个很好的补充。 (2认同)