I have two objects Object1 and Object2, which may or may not be null.
I want to test null safe equality between them (true if both null, or both equal)
I have done my research and found Objects.equals(Object1,Object2), which fulfills its purpose.
But the problem is that it's only for API level 19+. How do I make this compatible with lesser APIs?
There are several options.
You could override the
equals() method in the class you are comparing, and use obj1.equals(obj2)
You could use the hashCode() method of Object to determine if they are equivalent, by using obj1.hashCode() == obj2.hashCode()
Just using plain old == determines weather two variables contain the same instance of a class.
If these aren't null safe, you can easily write your own null checking with obj == null
This should be completely independent of the Android Api.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With