天天看點

咖啡汪日志——實際工作中好用的集合工具類(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