1. 程式人生 > 其它 >字串、object、集合、map等判空

字串、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)