使用CollectionUtils工具类判断集合是否为空
判断集合为空(List为null或size()==0)
1、CollectionUtils.isEmpty(null): true
2、例:List<String> a = new ArrayList<>(); //a.size()==0
1
|
CollectionUtils.isEmpty(a): true |
3、例:
1
2
3
4
5
|
List<String> list = new ArrayList<>(); list.add( "a" ); list.add( "b" ); System.out.println(list); //list.size()==2 CollectionUtils.isEmpty(list): false |
判断集合不为空(List不为null且size()!=0)
1、CollectionUtils.isNotEmpty(null): false
2、例:List<String> a = new ArrayList<>(); //a.size()==0
1
|
CollectionUtils.isNotEmpty(a): false |
3、例:
1
2
3
4
5
|
List<String> list = new ArrayList<>(); list.add( "a" ); list.add( "b" ); System.out.println(list); //list.size()==2 CollectionUtils.isNotEmpty(list): true |
判断集合是否为空底层
1
2
3
4
5
6
|
public static boolean isNotEmpty(Collection coll) { return !isEmpty(coll); } public static boolean isEmpty(Collection coll) { return coll == null || coll.isEmpty(); } |
集合判断是否为空工具类(CollectionUtils)
工具类
1
|
org.springframework.util.CollectionUtils |
方法
1
2
3
4
5
6
7
8
9
10
|
//Collection public static boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } //Map public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_35535042/article/details/115461147