有一个小需求,需要将两个集合中的元素求差集,这也是花了我不少功夫。现将集合常见的交集、并集、差集一些操作整理出来,有需要的小伙伴可以参考。
JDK8
自带的新特性我们都知道JDK8
对于我们日常开发起到了很大的遍历,特别是提供了函数式编程,以及对应的Stream
等。Java8 lambda
表达式流操作不影响原始list对象,所以这也是我们在日常开发当中常常使用的方式。
List<Student> list1 = new ArrayList<>();
list1.add(new Student("name100","100"));
list1.add(new Student("name102","102"));
list1.add(new Student("name103","103"));
List<Student> list2 = new ArrayList<>();
list2.add(new Student("name103","103"));
list2.add(new Student("name104","104"));
Set<Student> set1 = new HashSet<>(list1);
Set<Student> set2 = new HashSet<>(list2);
List<Student> intersection = list1.stream().filter(set2::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
list1--->list2
)List<Student> reduce1 = list1.stream().filter(item -> !set2.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
list2--->list1
)List<Student> reduce2 = list2.stream().filter(item -> !set1.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
List<Student> listAll = list1.parallelStream().collect(toList());
List<Student> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);
set1.addAll(set2);
List<Student> listDistinctAll = new ArrayList<>(set1);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
List<String> list1 = new ArrayList<>();
list1.add("1");
list1.add("2");
list1.add("3");
List<String> list2 = new ArrayList<>();
list2.add("3");
list2.add("4");
Set<String> set1 = new HashSet<>(list1);
Set<String> set2 = new HashSet<>(list2);
List<String> intersection = list1.stream().filter(set2::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
list1 - list2
)List<String> reduce1 = list1.stream().filter(item -> !set2.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
list2 - list1
)List<String> reduce2 = list2.stream().filter(item -> !set1.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);
set1.addAll(set2);
List<String> listDistinctAll = new ArrayList<>(set1);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
ArrayList
自带的方法List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
List1
中有的但是List2
中没有的元素) list1.removeAll(list2);
System.out.println(list1);
list1.addAll(list2);
System.out.println(list1);
list1.removeAll(list2);
list1.addAll(list2);
System.out.println(list1);
list1.retainAll(list2);
System.out.println(list1);