list根据对象的某个属性合并数据

List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
Person p1 = new Person();
p1.setId(1);
p1.setName(“a”);
list1.add(p1);
Person p2 = new Person();
p2.setId(2);
p2.setName(“b”);
list1.add(p2);
Person p3 = new Person();
p3.setId(2);
p3.setName(“c”);
list2.add(p3);
Person p4 = new Person();
p4.setId(3);
p4.setName(“d”);
list2.add(p4);
Person p5 = new Person();
p5.setId(1);
p5.setName(“w”);
list2.add(p5);
list1.addAll(list2);
System.out.println(list1);
list1=list1.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new));
System.out.println(list1);

你可能感兴趣的:(笔记,list,java,前端)