一.利用java8的新特性
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);
1).交集
List<Student> intersection = list1.stream().filter(set2::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
2).差集(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);
3).差集(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);
4).并集
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);
5).去重并集
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);
1).交集
List<String> intersection = list1.stream().filter(set2::contains).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
2).差集(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);
3).差集(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);
4).并集
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);
5).去重并集
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");
1). 差集(List1中有的但是List2中没有的元素)
list1.removeAll(list2);
System.out.println(list1);
2). 并集(不去重)
list1.addAll(list2);
System.out.println(list1);
3). 并集(去重)
list1.removeAll(list2);
list1.addAll(list2);
System.out.println(list1);
4). 交集
list1.retainAll(list2);
System.out.println(list1);