What is the purpose of
Objects.isNull(x)
if we can simply write
x == null
?
Same for
Objects.nonNull(...)
and
x != null
From the JavaDoc of the method:
API Note: This method exists to be used as a
Predicate,filter(Objects::isNull)
Elaborating the above answer further with an example. Given a list of strings, Objects.isNull and Objects.nonNull can be used to filter out the null and non null values respectively from the list.
List<String> stringList = Arrays.asList("T", null, "S");
// Filter out all the elements that are not null.
stringList.stream().filter(Objects::nonNull).collect(Collectors.toList()); // "T","S"
// Filter out all the null elements.
stringList.stream().filter(Objects::isNull).collect(Collectors.toList()); // null
This method exists to be used as a Predicate, filter(Objects::isNull).