您的位置 首页 java

java判断是否空的方法

java判断是否空的方法:

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的StringUtils.isEmpty(CharSequence cs)源码:

public static boolean isEmpty(final CharSequence cs) {        return cs == null || cs.length() == 0;}

org.springframework.util的StringUtils.isEmpty(Object str)源码:

public static boolean isEmpty(Object str) {        return (str == null || "".equals(str));}

基本上判断对象是否为空,StringUtils.isEmpty(Object str)这个方法都能搞定。

2、判断数组是否为空

list.isEmpty(); //返回boolean类型。

3、判断集合是否为空

CollectionUtils.isEmpty(null): trueCollectionUtils.isEmpty(new ArrayList()): trueCollectionUtils.isEmpty({a,b}): false

更多java知识请关注java基础教程栏目。

文章来源:智云一二三科技

文章标题:java判断是否空的方法

文章地址:https://www.zhihuclub.com/20401.shtml

关于作者: 智云科技

热门文章

网站地图