java.util.Objects.isNull vs object == null

前端 未结 5 1262
盖世英雄少女心
盖世英雄少女心 2020-12-08 03:57

As you know, java.util.Objects is

This class consists of static utility methods for operating on objects.

One of such methods i

5条回答
  •  青春惊慌失措
    2020-12-08 04:26

    Objects.isNull is intended for use within Java 8 lambda filtering.

    It's much easier and clearer to write:

    .stream().filter(Objects::isNull) 
    

    than to write:

    .stream().filter(x -> x == null).  
    

    Within an if statement, however, either will work. The use of == null is probably easier to read but in the end it will boil down to a style preference.

提交回复
热议问题