0
点赞
收藏
分享

微信扫一扫

下载安装:SQLite+SQLiteStudio+VS

沐之轻语 2023-07-27 阅读 36

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

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));

结果:

集合1false
集合11true
集合2false

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

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.4</version>
</dependency>

import org.apache.commons.collections4.CollectionUtils;
在这里插入图片描述

举报

相关推荐

0 条评论