Objects.isNull 判断失效引发的乱七八糟的想法

Objects.isNull 判断失效引发的乱七八糟的想法

1. 项目

  • 使用的是mybatis-plus, service.getMap()方法
  • 最初没有数据的时候是返回的{null}这样的数据结构
  • 但是我写这个日记的时候,抓取到的数据结构变成了null
  • 例:map数据========================>null=======================>true

    Map map = this.getMap(wrapper);
    if(Objects.isNull(map)) {
        return 0;
    }
    return 1;

2. 排查问题

  • 最初的判断是Objects.isNull() 导致判断的条件一直被穿透
  • 变成了返回结果为1,期待的返回结果应该是0才对
  • 引起了下面的代码测试

3. 代码

System.out.println("=========================>直接声明List类型==========================>");
List 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 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 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) )

你可能感兴趣的:(java,null,List)