import java.util.ArrayList; import java.util.List; import static java.util.stream.Collectors.toList;public class Code { public static void main(String[] args) throws ParseException { Listlist1 = new ArrayList(); list1.add("1"); list1.add("2"); list1.add("3"); list1.add("5"); list1.add("6"); Listlist2 = new ArrayList(); list2.add("2"); list2.add("3"); list2.add("7"); list2.add("8"); new Code().quchongjiaoji(list1,list2); } // 交集 public void jiaoji(Listlist1,List list2){ Listintersection = list1.stream().filter(item -> list2.contains(item)).collect(toList()); System.out.println("---交集 intersection---"); intersection.parallelStream().forEach(System.out :: println); } // 差集 (list1 - list2) public void chaji(Listlist1,List list2){ Listreduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList()); System.out.println("---差集 reduce1 (list1 - list2)---"); reduce1.parallelStream().forEach(System.out :: println); } // 差集 (list2 - list1) public void chajito(Listlist1,List list2){ Listreduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList()); System.out.println("---差集 reduce2 (list2 - list1)---"); reduce2.parallelStream().forEach(System.out :: println); } // 并集 public void binji(Listlist1,List list2){ ListlistAll = list1.parallelStream().collect(toList()); ListlistAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.println("---并集 listAll---"); listAll.parallelStream().forEachOrdered(System.out :: println); } // 去重并集 public void quchongjiaoji(Listlist1,List list2){ ListlistAll = list1.parallelStream().collect(toList()); ListlistAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); ListlistAllDistinct = listAll.stream().distinct().collect(toList()); System.out.println("---得到去重并集 listAllDistinct---"); listAllDistinct.parallelStream().forEachOrdered(System.out::println); } }