64

What is the purpose of

Objects.isNull(x) 

if we can simply write

x == null

?

Same for

Objects.nonNull(...)

and

x != null
2
  • I do not know but I guess OOP or Not OOP this is a question? Commented Jan 25, 2015 at 18:59
  • 12
    I don't know too, but the API Note may explain something: This method exists to be used as a Predicate, filter(Objects::isNull). Commented Jan 25, 2015 at 19:00

2 Answers 2

109

From the JavaDoc of the method:

API Note: This method exists to be used as a Predicate, filter(Objects::isNull)

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.