zoukankan      html  css  js  c++  java
  • 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空

    先引入CollectionUtils工具类:

    import org.apache.commons.collections4.CollectionUtils;

    工具类中的部分方法:

    public static boolean isEmpty(Collection<?> coll) {
    return coll == null || coll.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> coll) {
    return !isEmpty(coll);
    }

    测试:

    @Test
        public void test4(){
            boolean empty1 = CollectionUtils.isEmpty(null);
            System.out.println(empty1);
            boolean empty2 = CollectionUtils.isEmpty(new ArrayList());
            System.out.println(empty2);
            List list=new ArrayList();
            list.add("helloworld1");
            list.add("helloworld2");
            boolean empty3 = CollectionUtils.isEmpty(list);
            System.out.println(empty3);
            System.out.println("================================");
    
            boolean empty4 = CollectionUtils.isNotEmpty(null);
            System.out.println(empty4);
            boolean empty5 = CollectionUtils.isNotEmpty(new ArrayList());
            System.out.println(empty5);
            List list1=new ArrayList();
            list1.add("helloworld1");
            list1.add("helloworld2");
            boolean empty6 = CollectionUtils.isNotEmpty(list1);
            System.out.println(empty6);
        }

    结果为:

     项目中使用:

    List<AttachFile> fileList = attachFileService.getFileList(noteObj.getId(), "t_sys_notification", "attach_files");
    if (CollectionUtils.isNotEmpty(fileList)) {
    noteObj.setAttachFiles(fileList);
    }
    List<Variety> list = varietyMapper.selectByExample(example);
    if (CollectionUtils.isEmpty(list)) {
    return Result.operating("查询品种", true, ResultCode.SUCCESS, null);
    }

     通过StringUtils工具类判断字符串是否为空

    先引入StringUtils工具类:

    import org.apache.commons.lang3.StringUtils;

    工具类中的部分方法:

    public static boolean isEmpty(CharSequence cs) {
    return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
    return !isEmpty(cs);
    }

    在项目中的应用:

    if(StringUtils.isNotEmpty(materialName)){
    map.put("materialName", materialName);
    }
    if (!StringUtils.isEmpty(groupName)) {
    argMap.put("groupName", groupName);
    }
  • 相关阅读:
    Linux的五个查找命令
    Linux中ftp不能上传文件/目录的解决办法
    linux下的chmod,chown和chgrp
    Memcache基础教程
    Web服务器磁盘满故障
    linux定时任务生产java服务无法执行问题案例
    Linux 用户和用户组管理
    10-xargs 简明笔记
    51-du 显示关于目录层次结构或文件磁盘使用情况的信息
    46-df 显示磁盘空间的使用情况
  • 原文地址:https://www.cnblogs.com/zwh0910/p/13902986.html
Copyright © 2011-2022 走看看