1.List、Set
List、Set可以使用 org.apache.commons 提供的 CollectionUtils 方便的判空
List<String> studentIdList = new ArrayList<>();
CollectionUtils.isEmpty(studentIdList); // true
CollectionUtils.isNotEmpty(studentIdList); // false
原理也很简单
// 类名:org.apache.commons.collections4.CollectionUtils
public static boolean isEmpty(Collection<?> coll) {
return coll == null || coll.isEmpty();
}
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
2.Map
Map可以使用 org.apache.commons 提供的 MapUtils 进行判空
HashMap<String, Teacher> teacherIdEntityMap = new HashMap<>();
MapUtils.isEmpty(map); // true
MapUtils.isNotEmpty(map); // false
看看源码
// 类名:org.apache.commons.collections4.MapUtils
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}