【问题标题】:Sum all attribute values inside a list汇总列表中的所有属性值
【发布时间】:2019-09-17 19:07:23
【问题描述】:

我有一个具有多个 Integer 属性的类,类似这样:

public class Test {
    private Integer a;
    private Integer b;
    private Integer c;
    private Integer d;
    private Integer e;
    private Integer f;
    private Integer g;
}

所以,我得到了一个包含很多寄存器的这个类的列表,我必须单独总结这个列表的所有属性。然后我做了类似的事情:

List<Test> tests = testRepository.findAllTest();

Test test = new Test();
for (Test testList: tests) {
    test.setA(test.getA + testList.getA);
    test.setB(test.getB + testList.getB);
    test.setC(test.getC + testList.getC);
    test.setD(test.getD + testList.getD);
    test.setE(test.getE + testList.getE);
    test.setF(test.getF + testList.getF);
    test.setG(test.getG + testList.getG);
}
 return test;

这个实现工作正常,但我现在想如果有更简单的方法来做到这一点,清理这段代码并使其变得简单

【问题讨论】:

  • 我怀疑它做得很好,因为你在每一行都调用setA() :)。您可以将其包装在一个 add 方法中,例如 void addAttributes(Test other)
  • 使用包含 7 个 int 元素的数组,然后将它们添加到循环中。
  • Joakim Danielson 我编辑了,感谢您的观察

标签: java list arraylist


【解决方案1】:

我会将其分解为几个子问题:将一个 Test 对象添加到另一个对象,然后总结列表。

对于第一个问题,您可以向 Test 类添加一个方法,该方法添加两个测试对象并返回一个包含总和的新 Test 对象。

public class Test {
    ...

    public Test add(Test testToAdd){
        Test result = new Test();
        result.setA(a + testToAdd.getA());
        ...
        result.setG(g + testToAdd.getG());
        return result;
    }
}

然后你可以在求和循环中调用它:

List<Test> tests = testRepository.findAllTest();
Test testTotal = new Test();
for (Test test: tests) {
    testTotal = testTotal.add(test);
}

另一个好处是可以更直接地了解循环在做什么。

【讨论】:

  • 你也可以让 add 方法修改 Test 对象而不是返回一个新对象,然后在循环中你可以调用 testTotal.add(test)
【解决方案2】:

您可以修改您的 Test 类以包含一个 add 方法:

public class Test {
    private int a;
    private int b;
    private int c;
    //...

    public void add(Test o) {
        this.a += o.getA();
        this.b += o.getB();
        this.c += o.getC();
        //...
    }
    // setters and getters...
}

那么你的求和函数可以如下所示:

public Test summation(Collection<Test> testCollection) {
    Test sum = new Test();
    for(Test test : testCollection) {
        sum.add(test);
    }
    return sum;
}

【讨论】:

    【解决方案3】:

    使用Stream.reduce 为现有答案添加一种类似的方法:

    向您的测试类添加一个无参数构造函数(如果您还没有的话):

    private Test() {
        this(0,0,0,0,0,0,0);
    }
    

    将方法 addAttributes 添加到您的测试类

    public Test addAttributes(Test other){
        this.a += other.a; 
        this.b += other.b; 
        this.c += other.c; 
        this.d += other.d;
        //.... 
        return this;
    }
    

    然后您可以通过以下方式减少列表:

    Test result = tests.stream().reduce(new Test(), (t1,t2) -> t1.addAttributes(t2));
    

    【讨论】:

      【解决方案4】:

      Test 类中编写一个add(Test other) 方法:

      public void add(Test other) {
         this.a += other.getA();
         this.b += other.getB();
         // ...
         this.g += other.getG();
      }
      

      然后,使用它:

      Test test = new Test();
      List<Test> allTests = testRepository.findAllTest();
      allTests.forEach(individualTest -> individualTest.add(test));
      return test;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多