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;

相关推荐
qq_27049009614 分钟前
SpringBoot药品管理系统设计实现
java·spring boot·后端
、BeYourself43 分钟前
SpringAI-ChatClient Fluent API 详解
java·后端·springai
星辰_mya1 小时前
reids哨兵集群与选主
java·开发语言
BD_Marathon1 小时前
SpringBoot快速入门
java·spring boot·后端
期待のcode1 小时前
Java的多态
java·开发语言
证能量少女2 小时前
2026大专Java开发工程师,考什么证加分?
java·开发语言
FPGAI2 小时前
Java学习之基础概念
java·学习
芒克芒克2 小时前
Java集合框架总结(面试八股)
java·开发语言·面试
ejjdhdjdjdjdjjsl2 小时前
C#文件流操作技巧
java·开发语言·spring
虾说羊2 小时前
HashMap详解
java