通过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);
}
通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空的更多相关文章
- 关于hibernate的实体类中有集合类型转化成JSON的工具类 - 怀念今天的专栏 - 博客频道
Json 来源:http://blog.csdn.net/zczzsq/article/details/18697045#1536434-hi-1-4387-42d97150898b1af15ddaa ...
- Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)
集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...
- Java自学第6期——Collection、Map、迭代器、泛型、可变参数、集合工具类、集合数据结构、Debug
集合:集合是java中提供的一种容器,可以用来存储多个数据. 集合和数组既然都是容器,它们有啥区别呢? 数组的长度是固定的.集合的长度是可变的. 数组中存储的是同一类型的元素,可以存储基本数据类型值. ...
- 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。
package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...
- 利用StringUtils工具类进行String为空的判断
利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了. 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 下面是 St ...
- spring 工具类大集合
接以前的文章 apache-commons 常用工具类 和文章 apache-commons 工具类扩展 小家 Spring 对 spring 的工具类做了详细的介绍(一) 这里我抽出一些好用的类,不 ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- Java 知识笔记 - 类、集合、多线程、IO、JVM(最后一次更新,2019年02月17日)
目录 Class 内部类.静态内部类.匿名内部类.局部内部类 Collection Java Collection Set Queue Map Collections Arrays System Co ...
- 黑马程序员——【Java基础】——File类、Properties集合、IO包中的其他类
---------- android培训.java培训.期待与您交流! ---------- 一.File类 (一)概述 1.File类:文件和目录路径名的抽象表现形式 2.作用: (1)用来将文件或 ...
随机推荐
- Sqoop import export参数
通用参数 import export 通用通用参数选项 含义说明–connect 指定JDBC连接字符串–connection-manager 指定要使用的连接管理器类–dri ...
- 2019牛客暑期多校训练营(第二场)D Kth Minimum Clique(第k团)
题意:给你n个点 求第k小的团 思路:暴力bfs+bitset压位 #include <bits/stdc++.h> using namespace std; const int N = ...
- hdu3559 Frost Chain (概率dp+记忆化搜索)
Problem Description In the unimaginable popular DotA game, the hero Lich has a wonderful skill: Fros ...
- Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) A. Contest for Robots(数学)
题意: n 道题,2 个答题者,已知二者的做题情况,你是受贿裁判,可以给每题指定分值(≥1),求甲乙分数(甲>乙)相差最小时最大分值的最小值. 思路: 统计只有甲或乙做出的题目数. 加一取下整判 ...
- 【noi 2.6_7627】鸡蛋的硬度(DP)
题意:其中n表示楼的高度,m表示你现在拥有的鸡蛋个数. 解法:f[i][j]表示 i 层楼有 j 个鸡蛋时,至少要扔多少次.3重循环,k为测试的楼层,分这时扔下去的鸡蛋碎和不碎的情况.要注意初始化. ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)
题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...
- 创建java文件和注释
创建java文件和注释 一 创建java文件 在文件夹里创建txt文本文件,后将格式改为.java, 输入 1 public class Hello{ 2 public static void mai ...
- CS224--1:语言模型和词向量
参考: https://www.cnblogs.com/pinard/p/7243513.html https://blog.csdn.net/cindy_1102/article/details/8 ...
- Hexo、主题、部署上线
Hexo.主题.部署上线 安装Hexo git和nodejs安装好后,就可以安装hexo了,你可以先创建一个文件夹MyBlog,用来存放自己的博客文件,然后cd到这个文件夹下(或者在这个文件夹下直接右 ...