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;

相关推荐
大模型丫丫8 小时前
Loop Engineering:从强化学习视角看 Agent 循环的本质
java·人工智能·学习
数电发票API8 小时前
税局接口,需要的来~~
java
骇客野人8 小时前
IntelliJ IDEA 使用技巧
java·ide·intellij-idea
Jul1en_8 小时前
【Java 脚手架】封装通用工具类-1
java·spring boot·分布式·spring·spring cloud
关于不上作者榜就原神启动那件事9 小时前
从 MDC 到 Agent:我手搓的文档路由协议,在 Spring AI Alibaba 里找到了正式实现
java·人工智能·spring·ai·agent
亦暖筑序9 小时前
重新认识 AgentScope-Java 2.0:ReActAgent 负责推理,HarnessAgent 负责运行
java·后端·agent
木井巳9 小时前
【DFS解决floodfill算法】岛屿的最大面积
java·算法·leetcode·深度优先
运维大师10 小时前
【K8S 运维实战】34-多集群管理Karmada
java·运维·kubernetes
林森lsjs10 小时前
完结撒花!Java SE 语法阶段总结!
java·开发语言
lupai10 小时前
短信接口快速接入与调用实战指南
java·开发语言·数据库