Java 8 adding values of multiple property of an Object List

泪湿孤枕 提交于 2019-12-03 12:27:44

It seems that you need to group based on three things: Year, Month and Name, so this could look like this:

Collection<Target> merged = yourListOfTargets
            .stream()
            .collect(Collectors.toMap(
                    t -> List.of(t.getYear(), t.getMonth(), t.getName()),
                    Function.identity(),
                    (left, right) -> {
                        left.setTarget(left.getTarget() + right.getTarget());
                        left.setAchieved(left.getAchieved() + right.getAchieved());
                        return left;
                    }))
            .values();

As Federico mentions in comments, this will alter your elements in the initial List. You might be OK with it, but if you are not, you need to replace Function.identity() with a copying Function that would create a new Target from an existing one.

public class Test8 {
  static class Target {

    String year;
    int target, achieved;
    String month;
    String name;

    public Target(String string, String string2, String name, int achieved, int target) {

      this.year = string;
      this.month = string2;
      this.target = target;
      this.achieved = achieved;
      this.name = name;
    }

    @Override
    public String toString() {
      return "[year=" + year + 
          ", month=" + month 
          + ", target=" + target 
          + ", achieved=" + achieved
          + ", name="
          + name + "]";
    }

  }// target

  public static void main(String[] args) {

    List<Target> list = Arrays.asList(

        new Target("1993", "9", "Protijayi", 1000, 40), 
        new Target("1993", "9", "Protijayi", 600, 400),
        new Target("1987", "11", "Soudipta", 320, 200),
        new Target("1987", "11", "Soudipta", 500, 900),
        new Target("1985", "9", "Tabu", 300, 200),
        new Target("1986", "9", "Tabu", 700, 200)

    );


        Map<List<String>, Target> map = list
        .stream()
        .collect(
            Collectors.groupingBy(
                ch -> List.of(ch.year, ch.month, ch.name), 
                Collectors.collectingAndThen(

            Collectors.reducing(
    (a, b) -> new Target(a.year, a.month, a.name, a.target + b.target,a.achieved + b.achieved)

            ), Optional::get)

        ));
    System.out.println(" MAP ");
    System.out.println(map);
    System.out.println(".................");
    System.out.println(" MAP.VALUES() ");
    System.out.println(map.values());

  }// main

}

/*
  MAP 
{[1993, 9, Protijayi]=[year=1993, month=9, target=1600, achieved=440, name=Protijayi],
 [1985, 9, Tabu]=[year=1985, month=9, target=200, achieved=300, name=Tabu],
  [1987, 11, Soudipta]=[year=1987, month=11, target=820, achieved=1100, name=Soudipta],
   [1986, 9, Tabu]=[year=1986, month=9, target=200, achieved=700, name=Tabu]}
.................
 MAP.VALUES() 
[[year=1993, month=9, target=1600, achieved=440, name=Protijayi], 
[year=1985, month=9, target=200, achieved=300, name=Tabu],
[year=1987, month=11, target=820, achieved=1100, name=Soudipta],
 [year=1986, month=9, target=200, achieved=700, name=Tabu]]
*/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!