Objects.isNull 判断生效引发的乌七八糟的想法
1. 我的项目
- 应用的是mybatis-plus, service.getMap()办法
- 最后没有数据的时候是返回的{null}这样的数据结构
- 然而我写这个日记的时候,抓取到的数据结构变成了null
-
例:map数据========================>null=======================>true
Map<String, Object> map = this.getMap(wrapper); if(Objects.isNull(map)) { return 0; } return 1;
2. 排查问题
- 最后的判断是Objects.isNull() 导致判断的条件始终被穿透
- 变成了返回后果为1,期待的返回后果应该是0才对
- 引起了上面的代码测试
3. 代码
System.out.println("=========================>间接申明List类型==========================>");
List<Object> o = new ArrayList<>();
System.out.println(o.size() == 0); // true
System.out.println(o.isEmpty()); // true
System.out.println(Objects.isNull(o)); // false
System.out.println(Objects.equals(o,null)); // false
System.out.println(Objects.equals(o.size(),0)); // true
System.out.println("=========================>List变量赋值为null==========================>");
List<Object> o1 = null;
// System.out.println(o1.size() == 0); // Exception in thread "main" java.lang.NullPointerException
// System.out.println(o1.isEmpty()); // Exception in thread "main" java.lang.NullPointerException
System.out.println(Objects.isNull(o1)); // true
System.out.println(Objects.equals(o1,null)); // true
// System.out.println(Objects.equals(o1.size(),0)); // Exception in thread "main" java.lang.NullPointerException
System.out.println("=========================>间接申明map类型==========================>");
HashMap<String, Objects> m = new HashMap<>();
System.out.println(m.isEmpty()); // true
System.out.println(Objects.isNull(m)); // false
System.out.println(Objects.equals(m,null)); // false
System.out.println(Objects.equals(m.size(),0)); // true
4. 总结
是不是此类的判断都应该是先判断null,而后在具体的做判断????
if(Objects.nonNull(m) )
发表回复