java判断相关空

判断字符串为空

1、java.lang.string
    isEmpty 判断字符串值为空
    str.isEmpty()涉及兼容

先判断是否为对象,在判断引用是不是空字符串值
2if(str == null || str.equals(""))  

3、判断非空和非空白字符
if (str != null || !"".equals(str.trim())) {  
     //则字符串不为空或空格  
}  

判断非空(未判断非空白字符)
4ifstr != null && str.length() != 0) { }
   效率高

//org.apache.commons.lang3.StringUtils;
//Checks if a CharSequence is empty ("") or null
public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

//Checks if a CharSequence is whitespace, empty ("") or null.
public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
import java.util.List;
对象存在,判断size()==0,不存在报空指针
1import java.util.List;
   list.isEmpty()
   list.isEmpty() 和  list.size()==0 
   没有区别 ,isEmpty()判断有没有元素,而size()返回有几个元素, 如果判断一个集合有无元素 建议用isEmpty()方法.比较符合逻辑用法
2、如果想判断list是否为空,可以这么判断:
if(null == list || list.size() ==0 ){
  //为空的情况
}else{
  //不为空的情况
}

3、等价2
if(list!=null && !list.isEmpty()){
   //不为空的情况
}else{
   //为空的情况
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值