List<Stu> list = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.groupingBy(Stu::getId))
.entrySet()
.stream().map(Stu::create)
.collect(Collectors.toList());
class Stu {
private Integer id;
private Integer score;
public static Stu create(Map.Entry<Integer, List<Stu>> e) {
return new Stu(e.getKey(), e.getValue().stream().mapToInt(Stu::getScore).sum());
}
}
根据id为key建立一个hashmap啊,循环一轮就结束了。