java中判断list是否为空

java中判断list是否为空是日常代码中经常遇到的问题。最近发现一个Utils提供的方法可以一步判断。
废话不多说,直接上代码!

java 复制代码
ArrayList<String> arrayList = new ArrayList<>();
System.out.println("集合1:" + CollectionUtils.isNotEmpty(arrayList));
arrayList.add("1");
System.out.println("集合11:" + CollectionUtils.isNotEmpty(arrayList));


ArrayList<String> arrayList2 = null;
System.out.println("集合2:" + CollectionUtils.isNotEmpty(arrayList2));

结果:

java 复制代码
集合1:false
集合11:true
集合2:false

注意:需要pom文件需要引入

xml 复制代码
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.4</version>
</dependency>

import org.apache.commons.collections4.CollectionUtils;

相关推荐
魂梦翩跹如雨18 分钟前
数据库的“契约” —— 约束(Constrains)
java·数据库·mysql
独自破碎E43 分钟前
面试官:你有用过Java的流式吗?比如说一个列表.stream这种,然后以流式去处理数据。
java·开发语言
2601_949818091 小时前
头歌答案--爬虫实战
java·前端·爬虫
2601_949817921 小时前
大厂Java进阶面试解析笔记文档
java·笔记·面试
郭wes代码1 小时前
大三Java课设:一行行敲出来的贪吃蛇,老师以为我是CV的
java·开发语言
IGAn CTOU2 小时前
王炸级更新!Spring Boot 3.4 正式发布,新特性真香!
java·spring boot·后端
C雨后彩虹2 小时前
最多等和不相交连续子序列
java·数据结构·算法·华为·面试
tycooncool2 小时前
Spring中的IOC详解
java·后端·spring
014-code2 小时前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
CQU_JIAKE3 小时前
4.17[Q]
java·linux·服务器