在Java中,集合的非空判断需要同时考虑两种情况:集合对象是否为null 以及 集合是否为空(不含任何元素)。因为如果集合为null时直接调用其方法(如isEmpty()、size())会抛出NullPointerException,所以必须先判断null,再判断是否为空。
一、原生判断方式(不依赖工具类)
最基础的判断逻辑是:先判断集合是否为null,再通过isEmpty()方法判断是否为空集合(isEmpty()等价于size() == 0,但更直观)。
示例代码:
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class CollectionCheck {
public static void main(String[] args) {
// 1. 列表(List)的非空判断
List<String> list = new ArrayList<>();
// list = null; // 模拟集合为null的情况
// 判断:集合既不是null,也不是空集合
if (list != null && !list.isEmpty()) {
System.out.println("List非空,元素数量:" + list.size());
} else {
System.out.println("List为null或为空");
}
// 2. 映射(Map)的非空判断(逻辑相同)
Map<String, Integer> map = new HashMap<>();
// map = null; // 模拟集合为null的情况
if (map != null && !map.isEmpty()) {
System.out.println("Map非空,键值对数量:" + map.size());
} else {
System.out.println("Map为null或为空");
}
}
}
核心逻辑:
collection != null && !collection.isEmpty()
- 先通过
collection != null避免空指针异常; - 再通过
!collection.isEmpty()判断集合是否包含元素。
二、使用工具类简化判断(推荐)
频繁手写null和isEmpty()判断会导致代码冗余,实际开发中推荐使用成熟的工具类,如:
- Apache Commons Collections(
CollectionUtils) - Spring Framework(
CollectionUtils) - Guava(
Collections2)
1. Apache Commons Collections(最常用)
需引入依赖(Maven):
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
工具类方法:CollectionUtils.isNotEmpty(collection)
- 功能:判断集合既不是null,也不是空集合(等价于原生判断逻辑)。
- 反向方法:
CollectionUtils.isEmpty(collection)(判断集合为null或空)。
示例代码:
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
import java.util.ArrayList;
public class CollectionCheckWithTool {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
// list = null;
if (CollectionUtils.isNotEmpty(list)) { // 非空判断
System.out.println("List非空");
} else {
System.out.println("List为null或为空");
}
}
}
2. Spring Framework的CollectionUtils
如果项目已依赖Spring,可直接使用其org.springframework.util.CollectionUtils:
方法:CollectionUtils.isEmpty(collection)(判断为null或空)、CollectionUtils.isEmpty(map)(支持Map)。
示例代码:
import org.springframework.util.CollectionUtils;
import java.util.Map;
import java.util.HashMap;
public class SpringCollectionCheck {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
// map = null;
if (!CollectionUtils.isEmpty(map)) { // 非空判断(取反)
System.out.println("Map非空");
} else {
System.out.println("Map为null或为空");
}
}
}
3. Guava工具类
需引入依赖(Maven):
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
方法:com.google.common.collect.Collections2.isEmpty(collection)(判断为null或空)。
示例代码:
import com.google.common.collect.Collections2;
import java.util.Set;
import java.util.HashSet;
public class GuavaCollectionCheck {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
// set = null;
if (!Collections2.isEmpty(set)) { // 非空判断(取反)
System.out.println("Set非空");
} else {
System.out.println("Set为null或为空");
}
}
}
三、注意事项
-
区分
null和空集合:null:集合对象未初始化(不存在);- 空集合:对象已初始化(如
new ArrayList<>()),但不含元素。
两者都需要处理,否则可能导致业务逻辑错误(如误判“空集合”为有效数据)。
-
避免直接调用
size()判断空:
虽然size() == 0和isEmpty()效果相同,但isEmpty()更直观(语义上直接表达“是否为空”),推荐优先使用。 -
工具类的选择:
- 若项目无外部依赖,使用原生判断;
- 若已有Apache Commons或Spring依赖,优先使用对应工具类,减少重复代码。
通过以上方式,可安全、简洁地完成Java集合的非空判断,避免空指针异常和逻辑错误。
1846

被折叠的 条评论
为什么被折叠?



