@Test public void test1(){ List<Integer> list1=new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3); List<Integer> list2=new ArrayList<>(); list2.add(3); list2.add(4); list2.add(5); System.out.println("====求交集==="); List<Integer> list=list1.stream().filter(t->list2.contains(t)).collect(Collectors.toList()); list.stream().forEach(System.out::println); System.out.println("====求差集==="); list=list1.stream().filter(t-> !list2.contains(t)).collect(Collectors.toList()); list.stream().forEach(System.out::println); System.out.println("====求并集==="); list.addAll(list1); list.addAll(list2); list=list.stream().distinct().collect(Collectors.toList()); list.stream().forEach(System.out::println); }
原文:https://www.cnblogs.com/sword-successful/archive/2017/12/05/7989035.html