java基础之java非空判断
java非空判断是否为null是否为" "是否为空字符串(引号中间有空格)如: ""制表符、换行符、换页符和回车一. 字符串i为空非空if(str == null || str == " ")非空if(str ! =null&&str! =" ")if(str == null || str.isEmpty())if(str != null && !str.isEmp
·
java非空判断
- 是否为null
- 是否为""
- 是否为空字符串(引号中间有空格) 如: " "
- 制表符、换行符、换页符和回车
一. 字符串
| 为空 | 非空 |
|---|---|
| if(str == null || str == " ") | if(str ! =null&&str ! =" ") |
| if(str == null || str.isEmpty()) | if(str != null && !str.isEmpty()) |
| if(str == null || " ".equals(str.trim())) | if (str != null && !"".equals(str.trim())) |
| if(str == null || str.length()<=0) | if(str != null && str.length()>0) |
| if(str == null || “”.equals(str)) | if(str != null && !"".equals(str)) |
| if(StringUtils.isBlank(str)) | if(StringUtils.isNotBlank(str)) |
其中最后一个StringUtils 判断的是str.length()等价于第四个
二.数组
| 为空 | 不为空 |
|---|---|
| arr == null || (arr!=null &&arr.length==0) | arr!=null || (arr==null &&arr.length!=0) |
三.List集合
| 为空 | 非空 |
|---|---|
| iif(list == null || list.isEmpty()) | if(list != null && !list.isEmpty()) |
| if(list == null || list.size() == 0) | if(list != null && list.size() > 0) |
| if(list == null || StringUtils.isEmpty(list)) | if(list != null && !StringUtils.isEmpty(list)) |
| if (CollectionUtils.isEmpty(list)) | if (CollectionUtils.isNotEmpty(list)) |
其中第4个等价于第2个
四.Map
| 为空 | 非空 |
|---|---|
| if (MapUtils.isEmpty(map)) | if (MapUtils.isNotEmpty(map)) |
| if(map== null || map.size() == 0) | if(map!= null && map.size() > 0) |
| if(map== null || StringUtils.isEmpty(map)) | if(map!= null && !StringUtils.isEmpty(map)) |
其中第1个和第2个等价
五.null和isEmpty()的区别
- 这就相当于去商店买东西
- null 首先判断是否有商店(new ArrayList();)
- isEmpty()没有判断商店是否存在,而是判断商店是否有东西,如果连商店都没有,何来的的东西可卖(list.add(商品))
更多推荐




所有评论(0)