java非空判断

  1. 是否为 null
  2. 是否为 ""
  3. 是否为空字符串(引号中间有空格)  如: "     "。
  4. 制表符、换行符、换页符和回车

一. 字符串

1. if(str == null || str == "")                 非空 if(str != null && str != "")

2. if(str == null || str.isEmpty())                  if(str != null && !str.isEmpty())

3.if (str == null  || "".equals(str.trim()))       if (str != null && !"".equals(str.trim()))

4.if(str == null   || str.length()<=0)              if(str != null && str.length()>0)

5.if(str == null || "".equals(str))                  if(str != null && !"".equals(str))

6.if(StringUtils.isBlank(str))                       if(StringUtils.isNotBlank(str))                  import org.apache.commons.lang3.StringUtils;判断的是str.length(),相当于4

二.数组

1.arr==null || (arr!=null &&arr.length==0)           非空  arr!=null || (arr==null &&arr.length!=0) 

三.List集合

1.if(list == null || list.isEmpty())            非空  if(list != null && !list.isEmpty())

2.if(list == null || list.size() == 0)                    if(list != null && list.size() > 0)

3.if(list == null || StringUtils.isEmpty(list))     if(list != null && !StringUtils.isEmpty(list))

4.if (CollectionUtils.isEmpty(list))                  if (CollectionUtils.isNotEmpty(list))      等价于2

四.Map

1.if (MapUtils.isEmpty(map))                    非空  if (MapUtils.isNotEmpty(map))      等价于2

2.if(map== null || map.size() == 0)                     if(map!= null && map.size() > 0)

3.if(map== null || StringUtils.isEmpty(map))      if(map!= null && !StringUtils.isEmpty(map))

4.if(map== null || StringUtils.isEmpty(map))          if(map!= null && !StringUtils.isEmpty(map))

null和isEmpty()的区别

  1. 这就相当于去商店买东西
  2.  null 首先判断是否有商店(new ArrayList();)
  3.  isEmpty()没有判断商店是否存在,而是判断商店是否有东西,如果连商店都没有,何来的的东西可卖(list.add(商品))
原文地址:https://www.cnblogs.com/zhuyeshen/p/11984769.html