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

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


一、依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

二、案例

    @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());
    }

三、结果展示


        判断集合是否为空,只对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+1> 2。
  • 不是为了已经改写的历史。
  • 更不是为了将要开创的未来。
  • 唯一值得庆祝的。
  • 不是纸上的两个签名,而是找到一个千杯少的知己。
  • 我才华横溢,前途光明,有着足以改变世界的想法。
  • 投资我一定是您最一本万利的投资,这是您当年说的。
  • 我的一切努力都是为了兑现它。
  • 贵人,就是那个我不想令他失望的人。
  • 不问初心,方得始终。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

和烨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值