字符串、object、集合、map等判空
1、判断字符串或者对象是否为空
StringUtils的判断
StringUtils.isEmpty(CharSequence cs); //org.apache.commons.lang3包下的StringUtils类,判断是否为空的方法参数是字符序列类,也就是String类型
StringUtils.isEmpty(Object str); //而org.springframework.util包下的参数是Object类,也就是不仅仅能判断String类型,还能判断其他类型,比如Long等类型。
//org.apache.commons.lang3
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
//org.springframework.util
public static boolean isEmpty(Object str) {
return (str == null || "".equals(str));
}
StrUtil.isEmpty()
StrUtil.isEmptyIfStr()
2、判断数组、集合是否为空
list.isEmpty(); //判断数组返回boolean类型。
ArrayUtil.isEmpty()//判断数组是否为空
判断集合是否为空
// org.springframework.data.redis.support.collections.CollectionUtils
CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true
CollectionUtils.isEmpty({a,b}): false
//hutool
CollUtil.isEmpty(list)
4、判断map是否为空
MapUtil.isEmpty(userMap)
浙公网安备 33010602011771号