两个List对象多属性去重、交集、差集
public class Test {
public static void main(String[] args) {
People people1 = new People("admin","1","14");
People people2 = new People("admin","2","11");
People people5 = new People("zhangsan","1","11");
People people3 = new People("admin2","3","12");
People people4 = new People("admin","1","2");
People people6 = new People("zhangsan","2","2");
ArrayList<People> list = new ArrayList<>();
list.add(people1);
list.add(people2);
list.add(people5);
ArrayList<People> list2 = new ArrayList<>();
list2.add(people3);
list2.add(people4);
list2.add(people6);
List<People> updList = list.stream()
.filter(item -> list2.stream().map(e -> e.getCode() + "&" + e.getName())
.collect(Collectors.toList()).contains(item.getCode()+ "&" + item.getName()))
.collect(Collectors.toList());
List<People> updList2 = list.stream()
.filter(item -> !list2.stream().map(e -> e.getCode() + e.getName())
.collect(Collectors.toList()).contains(item.getCode() + item.getName()))
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(updList2));
list.addAll(list2);
List lst = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + "#" + o.getAge() ))),
ArrayList::new));
System.out.println(JSON.toJSONString(lst));
}
static class People{
private String name;
private String code;
private String age;
public People(String name, String code, String age) {
this.name = name;
this.code = code;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
}