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;

相关推荐
最强小杰24 分钟前
gpt-5.6-sol 频繁报 503 怎么办?区分容量熔断和限速 429 的排查方法 + 可复用 retry wrapper
java·人工智能·gpt·ai
吠品2 小时前
Wine 在 Linux 上运行 Windows 软件完整指南
java·linux·服务器
亚历克斯神3 小时前
智能搜索系统的升级复盘——从 Elasticsearch 到混合检索的检索质量提升
java·spring·微服务
金銀銅鐵4 小时前
[Java] 一个方法最多可以有多少个入参?
java·jvm
哭哭啼5 小时前
JAVA服务问题诊断
java·开发语言·jvm
深维AI随笔5 小时前
《构建之法》| 第二章个人技术和流程:单元测试不是“可选“,而是“必需“
单元测试·log4j·apache
Sayuanni%35 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
坚定信念,勇往无前5 小时前
Maven 私有仓库-nexus
java
Minner-Scrapy6 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
晚风醉蝶6 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法