咖啡汪日志——实际工作中好用的集合工具类(org.apache.commons.collections4.CollectionUtils)

本汪作为一名资深的哈士奇
每天除了闲逛,拆家,就是啃博客了
作为不是在戏精,就是在戏精的路上的二哈
今天就来给大家说说在实际工作中
collectionUtils 工具类正确使用姿势

一、collectionUtils推荐

org.apache.commons.collections4.CollectionUtils;

1、List集合判空


import org.apache.commons.collections4.CollectionUtils;

List<CouponTemplate> templates =
                templateDao.findAllByExpired(false);
        if (CollectionUtils.isEmpty(templates)) {
    
    
            log.info("Done To Expire CouponTemplate.");
            return;
        }
        if (CollectionUtils.isNotEmpty(expiredTemplates)) {
    
    
            log.info("Expired CouponTemplate Num: {}",
                    templateDao.saveAll(expiredTemplates));
        }


2、判断第一个list集合是否为第二个list集合的子集
判断 paramIds 是否为 curUsableIds 的子集

 List<Integer> curUsableIds = curUsableCoupons.stream().map(Coupon::getId).collect(Collectors.toList());
 List<Integer> paramIds = coupons.stream().map(Coupon::getId).collect(Collectors.toList());;
        if (CollectionUtils.isSubCollection(paramIds, curUsableIds)) {
    
    

        }


3、 数据库查询时 sql 中的 not in ,用 stream 流来实现(执行器记录表与执行器信息表不在一个数据库中)

获取当前可用的执行器 = 全部可用执行器 - 当前已使用用执行器
CollectionUtils.subtract(全集,子集),从全集中移除子集,

注意!集合中的元素,不可为对象类型引用,所以需要将对象转换为 String
进行集合的削减,再反序列化就好了。

/**
     * <h2>查询当前可用的执行器</h2>
     *
     * @return
     */
    @Override
    public List<SchedulerDto> getUsableScheduler() {
    
    

        //查询正在被使用的执行器信息从记录表中
        List<Cron> cronList = cronMapper.selectAllCronClassAndCronMethod();
        List<SchedulerDto> schedulerDtos = cronList.stream()
                .map(Cron::cron2SchedulerDto)
                .collect(Collectors.toList());

        //查询全部可用的执行器信息从执行器信息表中
        List<SchedulerDto> schedulerDtoList = schedulerMapper.selectAll();

        //无正在执行的,直接返回全部执行器
        if (CollectionUtils.isEmpty(schedulerDtos)) {
    
    
            return schedulerDtoList;
        }

        //无可用执行器,返回 null
        if (CollectionUtils.isEmpty(schedulerDtoList)) {
    
    
            return null;
        }

   //从全部可用执行器中移除正在使用的执行器,返回当前可用的执行器
        List<String> schedulerUsableStringList = schedulerDtos.stream()
                .map( c -> JSON.toJSONString(c))
                .collect(Collectors.toList());


        List<String> schedulerAllStringList = schedulerMapper.selectAll().stream()
                .map( c -> JSON.toJSONString(c))
                .collect(Collectors.toList());


        List<SchedulerDto> res = new LinkedList<>();

        if (CollectionUtils.isSubCollection(
       				 schedulerUsableStringList,
        			 schedulerAllStringList
        )) {
    
    
            Collection<String> collection = CollectionUtils.subtract(
		             schedulerAllStringList,
		             schedulerUsableStringList
            );
            collection.stream().forEach( s -> {
    
    
               res.add(JSON.parseObject(s,SchedulerDto.class));
            });
        }

        return res;
    }


4、取交集,判断商品类别中是否有可用的优惠券

// 存在交集即可,判断商品类型是否有可用的优惠券
        return CollectionUtils.isNotEmpty(
                CollectionUtils.intersection(goodsType, templateGoodsType)
        );

二、MapUtils org.apache.commons.collections4.MapUtils;

Map集合判空

 Map<Integer, Coupon> id2Coupon = coupons.stream()
                .collect(Collectors.toMap(
                        Coupon::getId,
                        Function.identity()
                ));
        if (MapUtils.isEmpty(id2Coupon)) {
    
    
            
        }

持续更新中。。。
一如既往,希望本汪的应用技术,能够在工作中,对大家有所帮助。

						————  咖啡汪  2020.12.9  

猜你喜欢

转载自blog.csdn.net/weixin_42994251/article/details/107281957