通过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);
}

通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空的更多相关文章

  1. 关于hibernate的实体类中有集合类型转化成JSON的工具类 - 怀念今天的专栏 - 博客频道

    Json 来源:http://blog.csdn.net/zczzsq/article/details/18697045#1536434-hi-1-4387-42d97150898b1af15ddaa ...

  2. Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)

    集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...

  3. Java自学第6期——Collection、Map、迭代器、泛型、可变参数、集合工具类、集合数据结构、Debug

    集合:集合是java中提供的一种容器,可以用来存储多个数据. 集合和数组既然都是容器,它们有啥区别呢? 数组的长度是固定的.集合的长度是可变的. 数组中存储的是同一类型的元素,可以存储基本数据类型值. ...

  4. 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。

    package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  5. 利用StringUtils工具类进行String为空的判断

      利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了.   判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0   下面是 St ...

  6. spring 工具类大集合

    接以前的文章 apache-commons 常用工具类 和文章 apache-commons 工具类扩展 小家 Spring 对 spring 的工具类做了详细的介绍(一) 这里我抽出一些好用的类,不 ...

  7. StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)

      在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取   StringUtils ...

  8. Java 知识笔记 - 类、集合、多线程、IO、JVM(最后一次更新,2019年02月17日)

    目录 Class 内部类.静态内部类.匿名内部类.局部内部类 Collection Java Collection Set Queue Map Collections Arrays System Co ...

  9. 黑马程序员——【Java基础】——File类、Properties集合、IO包中的其他类

    ---------- android培训.java培训.期待与您交流! ---------- 一.File类 (一)概述 1.File类:文件和目录路径名的抽象表现形式 2.作用: (1)用来将文件或 ...

随机推荐

  1. 杂论-FTP

    FTP 一 简单介绍 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制文件的双向传输.同时, ...

  2. python flask_sqlalchemy 多态 polymorphic 实现单表继承

    sqlalchemy 多态 polymorphic 实现单表继承 sqlaclchemy中的单表继续就是以一个模型类为基类,其他模型类继承基类,所有模型类的的数据都存一张表里面(也可以是多张,只不过基 ...

  3. springboot注解开发

    可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项 ...

  4. vscode开发vue,热更新

    1.首先用vscode去安装热更新插件 2.vscode安装后默认修改的文件是没有开启自动保存的,需要将自动保存勾选 这样就不用每次修改都去open with live server:

  5. left join与inner join

    举例a.b表: bId bNum 1 20 2 30 aId aNum 1 10 2 20 3 30 left join以左表为准 select * from a left join b on a.a ...

  6. codeforces 86D D. Powerful array

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

  7. hdu 1517 Multiplication Game

    题意: 用整数p乘以2到9中的一个数字.斯坦总是从p = 1开始,做乘法,然后奥利乘以这个数,然后斯坦,以此类推.游戏开始前,他们画一个整数1 < n < 4294967295,谁先到达p ...

  8. codeforces578C. Weakness and Poorness

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. ElasticSearch入门到筋痛

    1. 什么是ES ES中文网:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html ES:是一款基于Lucene ...

  10. Open3d之交互式可视化

    本篇教程介绍了Open3D的可视化窗口的交互功能. # -*- coding:utf-8 -*- import copy import numpy as np import open3d as o3d ...