咖啡汪日志——实际工作中好用的集合工具类(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  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于SpringMVC+Hibernate4的考勤管理系统+.zip 项目环境 系统:Windows7 X64位系统 IDE:Intellij IDEA 14.0 后端技术选型 J D K 版 本:JDK 1.8 数 据 库:Mysql 5.7 WEB容器:Tomcat 7.0 视图框架:SpringMVC 4.3.6.RELEASE 核心框架:Spring Framework 4.3.6.RELEASE 持久层框架:Hibernate4.3.11.Final 数据库连接池:C3P0 工具类 Apache fileupload 文件上传组件 Apache commons-collections 封装好的各种集合类和集合工具类 Apache commons-io Apache基金会创建并维护的Java函数库 Apache commons-logging 通用的日志接口 dom4j 优秀的JavaXML API 主要用于解析XML文档 poi组件 主要用于读取以及写入Microsoft Office格式档案 JSR 303 为实体验证定义了一个元数据模型和API 前端技术选型 JS框架:jQuery 1.8 CSS框架:Twitter Bootstrap 项目所需jar包列表 antlr-2.7.7.jar classmate-1.0.0.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-collections4-4.0.jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-logging-1.1.3.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-entitymanager-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar hibernate-validator-5.1.3.Final.jar hibernate-validator-annotation-processor-5.1.3.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar jstl-1.2.jar mysql-connector-java-5.1.7-bin.jar poi-3.15.jar poi-examples-3.15.jar poi-excelant-3.15.jar poi-ooxml-3.15.jar poi-ooxml-schemas-3.15.jar poi-scratchpad-3.15.jar spring-aop-4.0.0.RELEASE.jar 使用Spring 的AOP 特性时所需的类和源码级元数据支持 spring-aspects-4.0.0.RELEASE.jar 提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中 spring-beans-4.0.0.RELEASE.jar 所有应用都要用到的,它包含访问配置文件、创建和管理bean spring-context-4.0.0.RELEASE.jar Spring 核心提供了大量扩展 spring-core-4.0.0.RELEASE.jar Spring 框架基本的核心工具类,是其它组件的基本核心 spring-expression-4.0.0.RELEASE.jar Spring表达式语言 spring-jdbc-4.0.0.RELEASE.jar 对Spring 对JDBC 数据访问进行封装的所有类 spring-orm-4.0.0.RELEASE.jar Spring对DAO特性集进行了扩展 spring-tx-4.0.0.RELEASE.jar 事务相关的类 spring-web-4.0.0.RELEASE.jar Web 应用开发时,用到Spring 框架时所需的核心类 spring-webmvc-4.0.0.RELEASE

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

咖啡汪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值