javaList 交、并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的javaList对象,采用java8 lambda表达式流操作则可以不影响原始list对象而得到两个javaList对象的 交、并、差集。
1、简单两个List<String> 交、并、差集
/** * 简单类型的处理 */ public static void showSimpleDeal() { List<String> list1 = new ArrayList<>(); list1.add("1111"); list1.add("2222"); list1.add("3333"); List<String> list2 = new ArrayList<>(); list2.add("3333"); list2.add("4444"); Set<String> list1Set = new HashSet<>(list1); Set<String> list2Set = new HashSet<>(list2); // 交集 List<String> intersection = list1.stream().filter(list2Set::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---"); intersection.parallelStream().forEach(System.out::println); // 差集 (list1 - list2) List<String> 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<String> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---"); reduce2.parallelStream().forEach(System.out::println); // 并集 List<String> listAll = list1.parallelStream().collect(Collectors.toList());
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2); System.out.println("---得到并集 listAll---"); listAll.parallelStream().forEach(System.out::println); // 去重并集 list1Set.addAll(list2Set); List<String> 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<T> 与List<String>交、差集
static class User { public String name = ""; public int age=0; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void main(String[] args) { List<User> list1 = new ArrayList(); User user1=new User(); user1.setName("A运营"); user1.setAge(12); list1.add(user1); User user2=new User(); user2.setName("B运营"); user2.setAge(22); list1.add(user2); User user3=new User(); user3.setName("E运营"); user3.setAge(32); list1.add(user3);; List<String> list2 = new ArrayList(); list2.add("B运营"); list2.add("C运营"); list2.add("D运营"); List<String> nameList= list1.stream().map(User::getName).collect(Collectors.toList()); // 交集 List<User> 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<User> 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<String> 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<T> 交、并、差集
/** * 对象类型的处理 */ public static void showObjectDeal() { List<User> list1 = new ArrayList<>(); list1.add(new User("name1",11));
list1.add(new User("name2",22));
list1.add(new User("name3",33));
List<User> list2 = new ArrayList<>(); list2.add(new User("name3",33));
list2.add(new User("name4",44));
Set<User> list1Set = new HashSet<>(list1); Set<User> list2Set = new HashSet<>(list2); // 交集 List<User> intersection = list1.stream().filter(list2Set::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---"); intersection.parallelStream().forEach(System.out::println); // 差集 (list1 - list2) List<User> 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<User> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---"); reduce2.parallelStream().forEach(System.out::println); // 并集 List<User> listAll = list1.parallelStream().collect(Collectors.toList());
List<User> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2); System.out.println("---得到并集 listAll---"); listAll.parallelStream().forEach(System.out::println); // 去重并集 list1Set.addAll(list2Set); List<User> 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<User> 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()); }