使用Java 8 的stream计算交集并集差集

方法 / 步骤

基本元素的操作

        List<Integer> sourceList =new ArrayList<>();
        sourceList.add(1);
        sourceList.add(2);
        sourceList.add(3);
        List<Integer> targetList=new ArrayList<>();
        targetList.add(3);
        targetList.add(4);
        targetList.add(5);

        //求与目标List的交集
        List<Integer> noChangeIds = sourceList.stream().filter(source -> targetList.contains(source)).collect(Collectors.toList());
        System.out.println("noChangeIds_"+noChangeIds.toString());
        
        //求与目标List的差集
        List<Integer> waitDelIds = sourceList.stream().filter(source -> !targetList.contains(source)).collect(Collectors.toList());
        System.out.println("waitDelIds" + waitDelIds.toString());
        
        //求与原list的差集
        List<Integer> waitInsert = targetList.stream().filter(target -> !sourceList.contains(target)).collect(Collectors.toList());
        System.out.println("waitInsert" + waitInsert.toString());

对象元素的操作

        /**
         * List对象中的交集并集差集
         */
        List<Person> personList = new ArrayList<>();
        Person person1 = new Person(1L,"小明",1,10);
        Person person2 = new Person(2L,"小红",2,11);
        Person person3 = new Person(3L,"小兰",2,10);
        Person person4 = new Person(3L,"小兰",2,10);



        personList.add(person4);
        personList.add(person3);
        personList.add(person2);
        personList.add(person1);
        personList.forEach(temp-> System.out.println(temp.toString()));
        /**
         * 去除重复的对象
         */
        //方法一:
        List<Person> distinctPersonList = removeReObjectById(personList);
        //方法二:
//        List distinctPersonList = personList.stream().collect(Collectors.collectingAndThen(Collectors
//                                    .toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new));
        distinctPersonList.forEach(temp-> System.out.println("distinctPersonList_"+temp.toString()));
        System.out.println("======================================================");

        /**
         * 找出list中的对象,对应的参数出现了几次
         */
        Map<Long, Long> idRecount = personList.stream().collect(Collectors.groupingBy(person -> person.getId(), Collectors.counting()));
        idRecount.forEach((k,v)-> System.out.println("PersonID : "+k+"在List中出现了"+v+"次"));

        //找出List中重复的对象
        List<Long> reIds = new ArrayList<>();
        idRecount.forEach((k,v)->{
     if(v > 1){
      reIds.add(k);}});
        List<Person> findReObjectById = personList.stream().filter(tempPerson -> reIds.contains(tempPerson.getId()))
                                        .collect(Collectors.collectingAndThen(Collectors
                                        .toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new));
        findReObjectById.forEach(temp-> System.out.println("findReObjectByIdList"+temp.toString()));
    }

    private List<Person> removeReObjectById(List<Person> personList){
     
        Set<Person> peopleTreeSet = new TreeSet<>(Comparator.comparing(Person::getId));
        peopleTreeSet.addAll(personList);
        return new ArrayList<>(peopleTreeSet);
    }

参考资料 & 致谢

【1】Java8特性详解 lambda表达式 Stream
【2】java8新特性5:深入理解Java8 Lambda表达式

你可能感兴趣的:(Spring,/,Boot,/,OtherFramework,Java,/,C++,/,C#,....)