Using `Objects.equals()` in Android

android, java

Solution

The javadoc for `Objects.equals(obj a, obj b)` says: Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

which is the equivalent to:

if (a == null && b == null) {
   return true; 
} else if (a == null || b == null) {
   return false; 
} else return a.equals(b);

Problem

I am trying to use the `Objects.equals(obj a, obj b)` method (link) in Android, but it seems Android does not have access to it. As far as I'm aware, this class was available in Java 1.7 and later. Is there any way to have access to this class in Android? Or is there an equivalent method that behaves the same way that I can use instead?

Original source