• 学习集合工具类CollectionUtils——List对象案例


    学习集合工具类CollectionUtils——List对象案例


    一、依赖

            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-collections4artifactId>
                <version>4.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、案例

        @Test
        void test17() {
            People[] peopleArray = new People[2];
            for (int i = 0; i < peopleArray.length; i++) {
                peopleArray[i] = new People(i, String.valueOf(i), i);
            }
    
            List<People> peopleList1 = Lists.newArrayList();
            peopleList1.add(new People(1, "小李", 3));
            peopleList1.add(new People(2, "小张", 2));
    
            List<People> peopleList2 = Lists.newArrayList();
            peopleList2.add(new People(1, "小李", 3));
            peopleList2.add(new People(3, "小皇", 1));
    
            List<People> peopleList3 = Lists.newArrayList();
    
            log.info("判断集合是否为空,只对Collection及子类有效");
            boolean isEmpty = CollectionUtils.isEmpty(peopleList1);
            log.info(String.valueOf(isEmpty));
    
            log.info("判断集合是否不为空,只对Collection及子类有效");
            boolean isNotEmpty = CollectionUtils.isNotEmpty(peopleList1);
            log.info(String.valueOf(isNotEmpty));
    
            log.info("如果参数2处不为null,把参数2添加到peopleList3集合中");
            boolean AddIgnoreNull = CollectionUtils.addIgnoreNull(peopleList3, new People(4, "小蓝", 0));
            log.info(String.valueOf(AddIgnoreNull));
            log.info(peopleList3.toString());
    
            log.info("从peopleList1中删除peopleList2");
            List<People> removeAll = CollectionUtils.removeAll(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(removeAll.toString());
    
            log.info("获取并集");
            List<People> union = CollectionUtils.union(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(union.toString());
    
            log.info("获取交集");
            List<People> intersection = CollectionUtils.intersection(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(intersection.toString());
    
            log.info("获取交集的补集");
            List<People> disjunction = CollectionUtils.disjunction(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(disjunction.toString());
    
            log.info("获取差集");
            List<People> subtract = CollectionUtils.subtract(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(subtract.toString());
    
            log.info("返回peopleList2在peopleList1中的数据");
            List<People> retainAll = CollectionUtils.retainAll(peopleList1, peopleList2).stream().collect(Collectors.toList());
            log.info(retainAll.toString());
    
            log.info("数组反转");
            CollectionUtils.reverseArray(peopleArray);
            log.info(Arrays.stream(peopleArray).collect(Collectors.toList()).toString());
    
            log.info("返回每个元素出现的个数");
            Map<People, Integer> cardinalityMap = CollectionUtils.getCardinalityMap(peopleList1);
            log.info(cardinalityMap.toString());
    
            log.info("返回对象在集合中出现的次数");
            int cardinality = CollectionUtils.cardinality(new People(1, "小李", 3), peopleList1);
            log.info(String.valueOf(cardinality));
    
            log.info("返回结合指定位置的数");
            People getPeople = CollectionUtils.get(peopleList1, 0);
            log.info(getPeople.toString());
    
            log.info("判断两个集合是否相等");
            boolean equalCollection = CollectionUtils.isEqualCollection(peopleList1, peopleList2);
            log.info(String.valueOf(equalCollection));
    
            log.info("判断集合1是否小于集合2");
            boolean properSubCollection = CollectionUtils.isProperSubCollection(peopleList1, peopleList2);
            log.info(String.valueOf(properSubCollection));
    
            log.info("判断是否是子集");
            boolean subCollection = CollectionUtils.isSubCollection(peopleList1, peopleList2);
            log.info(String.valueOf(subCollection));
    
    
            log.info("判断是否存在交集");
            boolean containsAny = CollectionUtils.containsAny(peopleList1, peopleList2);
            log.info(String.valueOf(containsAny));
    
            log.info("根据条件筛选集合元素");
            List<People> collect = CollectionUtils.select(peopleList1, new Predicate<People>() {
                @Override
                public boolean evaluate(People people) {
                    if (people.getId() % 2 == 0) {
                        return true;
                    }
                    return false;
                }
            }).stream().collect(Collectors.toList());
            log.info(collect.toString());
    
            log.info("根据指定方法处理集合元素,类似List的map()");
            CollectionUtils.transform(peopleList1, new Transformer<People, People>() {
                @Override
                public People transform(People people) {
                    people.setName(people.getName() + "_update");
                    return people;
                }
            });
            log.info(peopleList1.toString());
    
            log.info("基本和select一样");
            People findPeople = CollectionUtils.find(peopleList1, new Predicate<People>() {
                @Override
                public boolean evaluate(People people) {
                    if (people.getId() % 2 != 0) {
                        return true;
                    }
                    return false;
                }
            });
            log.info(findPeople.toString());
    
            log.info("调用每个元素的指定方法");
            CollectionUtils.forAllDo(peopleList1, new Closure<People>() {
                @Override
                public void execute(People people) {
                    people.setName(people.getName() + "_update");
                }
            });
            log.info(peopleList1.toString());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    三、结果展示

    
            判断集合是否为空,只对Collection及子类有效
            false
            
            判断集合是否不为空,只对Collection及子类有效
            true
            
            如果参数2处不为null,把参数2添加到peopleList3集合中
            true
            [People(id=4, name=小蓝, jgId=0)]
            
            从peopleList1中删除peopleList2
            [People(id=2, name=小张, jgId=2)]
            
            获取并集
            [People(id=1, name=小李, jgId=3), People(id=2, name=小张, jgId=2), People(id=3, name=小皇, jgId=1)]
            
            获取交集
            [People(id=1, name=小李, jgId=3)]
            
            获取交集的补集
            [People(id=2, name=小张, jgId=2), People(id=3, name=小皇, jgId=1)]
            
            获取差集
            [People(id=2, name=小张, jgId=2)]
            
            返回peopleList2在peopleList1中的数据
            [People(id=1, name=小李, jgId=3)]
            
            数组反转
            [People(id=1, name=1, jgId=1), People(id=0, name=0, jgId=0)]
            
            返回每个元素出现的个数
            {People(id=1, name=小李, jgId=3)=1, People(id=2, name=小张, jgId=2)=1}
            
            返回对象在集合中出现的次数
            1
            
            返回结合指定位置的数
            People(id=1, name=小李, jgId=3)
            
            判断两个集合是否相等
            false
            
            判断集合1是否小于集合2
            false
            
            判断是否是子集
            false
            
            判断是否存在交集
            true
            
            根据条件筛选集合元素
            [People(id=2, name=小张, jgId=2)]
            
            根据指定方法处理集合元素,类似Listmap()
            [People(id=1, name=小李_update, jgId=3), People(id=2, name=小张_update, jgId=2)]
            
            基本和select一样
            People(id=1, name=小李_update, jgId=3)
            
            调用每个元素的指定方法
            [People(id=1, name=小李_update_update, jgId=3), People(id=2, name=小张_update_update, jgId=2)]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 即使慢也要驰而不息。
    • 为梦想追逐,随时可以上路。
    • 走上坡的时候要对别人好一点,因为你走下坡的时候会碰到他。
    • 宁愿失败地做你爱做的事情,也不要成功地做你恨做的事情。
    • 即使走的慢也决不后退。
    • 有人像家雀儿不愿意挪窝,有人像候鸟永远在路上。
    • 用自己的双脚在这土地上占据一个位置,无论是踩在泥泞里还是荆棘上。
    • 不说什么强强联手,不谈什么1+1> 2。
    • 不是为了已经改写的历史。
    • 更不是为了将要开创的未来。
    • 唯一值得庆祝的。
    • 不是纸上的两个签名,而是找到一个千杯少的知己。
    • 我才华横溢,前途光明,有着足以改变世界的想法。
    • 投资我一定是您最一本万利的投资,这是您当年说的。
    • 我的一切努力都是为了兑现它。
    • 贵人,就是那个我不想令他失望的人。
    • 不问初心,方得始终。
  • 相关阅读:
    Efficient Batched Oblivious Transfer Protocol
    Fhopify:跨境电商行业迎来发展新机遇打造购物者天堂
    使用双动态令牌混合器学习全局和局部动态以进行视觉识别
    力扣--在排序数组中查找元素的第一个和最后一个位置
    【工具推荐】替换typora的又一款神器
    C++11标准模板(STL)- 算法(std::partial_sort)
    .Net Core AOP之AuthorizeAttribute
    MySQL数据库简介+库表管理操作+数据库用户管理
    【Python】爬虫-基础入门
    Hexagon_V65_Programmers_Reference_Manual(29)
  • 原文地址:https://blog.csdn.net/weixin_46146718/article/details/127429005