java中判斷l(xiāng)ist是否為空是日常代碼中經(jīng)常遇到的問題。最近發(fā)現(xiàn)一個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));
結果:
集合1:false
集合11:true
集合2:false
注意:需要pom文件需要引入文章來源:http://www.zghlxwxcb.cn/news/detail-609958.html
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
import org.apache.commons.collections4.CollectionUtils;
文章來源地址http://www.zghlxwxcb.cn/news/detail-609958.html
到了這里,關于java中判斷l(xiāng)ist是否為空的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!