1.cn.hutool.core.util包下的ObjectUtil.isEmpty()
/**
* 判断指定对象是否为空,支持:
*
* <pre>
* 1. CharSequence
* 2. Map
* 3. Iterable
* 4. Iterator
* 5. Array
* </pre>
*
* @param obj 被判断的对象
* @return 是否为空,如果类型不支持,返回false
* @since 4.5.7
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object obj) {
if (null == obj) {
return true;
}
if (obj instanceof CharSequence) {
return StrUtil.isEmpty((CharSequence) obj);
} else if (obj instanceof Map) {
return MapUtil.isEmpty((Map) obj);
} else if (obj instanceof Iterable) {
return IterUtil.isEmpty((Iterable) obj);
} else if (obj instanceof Iterator) {
return IterUtil.isEmpty((Iterator) obj);
} else if (ArrayUtil.isArray(obj)) {
return ArrayUtil.isEmpty(obj);
}
return false;
}
2.org.springframework.util包下的ObjectUtil.isEmpty()
public static boolean isEmpty(@Nullable Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof Optional) {
return !((Optional<?>) obj).isPresent();
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof Collection) {
return ((Collection<?>) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map<?, ?>) obj).isEmpty();
}
// else
return false;
}