javaList 交、并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的javaList对象,采用java8 lambda表达式流操作则可以不影响原始list对象而得到两个javaList对象的 交、并、差集。
1、简单两个List 交、并、差集
/*** 简单类型的处理*/
public static voidshowSimpleDeal() {
List list1 = new ArrayList<>();
list1.add("1111");
list1.add("2222");
list1.add("3333");
List list2 = new ArrayList<>();
list2.add("3333");
list2.add("4444");
Set list1Set = new HashSet<>(list1);
Set list2Set = new HashSet<>(list2);//交集
List intersection =list1.stream().filter(list2Set::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);//差集 (list1 - list2)
List reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);//差集 (list2 - list1)
List reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);//并集
List listAll =list1.parallelStream().collect(Collectors.toList());
List listAll2 =list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);//去重并集
list1Set.addAll(list2Set);
List listDistinctAll = new ArrayList<>(list1Set);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEach(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEach(System.out::println);
}
2、List 与List交、差集
static classUser {public String name = "";public int age=0;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
}public static voidmain(String[] args) {
List list1 = newArrayList();
User user1=newUser();
user1.setName("A运营");
user1.setAge(12);
list1.add(user1);
User user2=newUser();
user2.setName("B运营");
user2.setAge(22);
list1.add(user2);
User user3=newUser();
user3.setName("E运营");
user3.setAge(32);
list1.add(user3);;
List list2 = newArrayList();
list2.add("B运营");
list2.add("C运营");
list2.add("D运营");
List nameList=list1.stream().map(User::getName).collect(Collectors.toList());//交集
List intersection = list1.stream().filter(item ->list2.contains(item.getName())).collect(Collectors.toList());
System.out.println("---得到交集 intersection---");//intersection.parallelStream().forEach(System.out::println);
for(User user: intersection) {
System.out.println(user.getName());
}//差集 (list1 - list2)
List reduceUser1=list1.stream().filter(user -> !list2.contains(user.getName())).collect(Collectors.toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");for(User user: reduceUser1) {
System.out.println(user.getName());
}//reduceUser1.parallelStream().forEach(System.out::println);//差集 (list2 - list1)
list1.stream().map(user ->user.getName()).collect(Collectors.toList());
List reduce2 = list2.stream().filter(item -> !nameList.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
}
3 两个相同类型List 交、并、差集
/*** 对象类型的处理*/
public static voidshowObjectDeal() {
List list1 = new ArrayList<>();
list1.add(new User("name1",11));
list1.add(new User("name2",22));
list1.add(new User("name3",33));
List list2 = new ArrayList<>();
list2.add(new User("name3",33));
list2.add(new User("name4",44));
Set list1Set = new HashSet<>(list1);
Set list2Set = new HashSet<>(list2);//交集
List intersection =list1.stream().filter(list2Set::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);//差集 (list1 - list2)
List reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);//差集 (list2 - list1)
List reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);//并集
List listAll =list1.parallelStream().collect(Collectors.toList());
List listAll2 =list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);//去重并集
list1Set.addAll(list2Set);
List listDistinctAll = new ArrayList<>(list1Set);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEach(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEach(System.out::println);
}
4、list去重
//List去重
List userList = list1.stream() .collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getName()))), ArrayList::new));
System.out.println("---List去重---");
for (User user: userList) {
System.out.println(user.getName());
}