2 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
似乎您需要根據(jù)三件事進(jìn)行分組:Year,Month和Name,因此這可能如下所示:
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();
正如 Federico 在評(píng)論中提到的,這將改變您在初始List. 您可能會(huì)接受它,但如果不是,則需要替換Function.identity()為一個(gè)復(fù)制功能,該功能Target將從現(xiàn)有功能中創(chuàng)建一個(gè)新功能。
添加回答
舉報(bào)