Why is SparseIntArray.equals (Object) not working?
I am using SparseIntArray
, and I am puzzled by this behavior:
public static SparseIntArray getArray()
{
SparseIntArray result = new SparseIntArray();
result.append(0, 99);
result.append(1, 988);
result.append(2, 636);
return result;
}
public static void testArray()
{
SparseIntArray first = getArray();
SparseIntArray second = getArray();
if( first.equals(second) )
{
Log.v(TAG,"first "+first.toString()+" == second "+second.toString());
}
else
{
Log.v(TAG,"first "+first.toString()+" != second "+second.toString());
}
}
Output:
11-06 14:53:15.011: V/fileName(6709): first {0=99, 1=988, 2=636} != second {0=99, 1=988, 2=636}
I know that using == between two objects will compare the addresses of the objects, which are different in this case, but I am using here SparseIntArray.equals(Object other)
and the intended result is not unexpected.
I'm sure I can upset my comparison method, but that sounds silly. What's the point of having a base class method Object.equals(Object other)
if we can't rely on it?
Can anyone point out any error?
source to share
I was just looking for the code SparseIntArray
. If you refer to android.util.SparseIntArray it does not override equals
, which means it uses the standard implementation of the Object
class that compares references.
What is the point of using the base class's Object.equals method (Object other) if we can't rely on it?
You can't really rely on the base Object.equals class, as it does exactly what you don't want to do:
public boolean equals(Object obj)
{
return (this == obj);
}
It is up to the writers of any class to decide whether to override equals
and execute another implementation.
source to share
@Eran is right, Object.equals (Object) does not shortcut it.
I made a simple static method to compare two instances
public static boolean compareSame( SparseIntArray first, SparseIntArray second )
{
// compare null
if( first == null )
{
return (second == null);
}
if( second == null )
{
return false;
}
// compare count
int count = first.size();
if( second.size() != count )
{
return false;
}
// for each pair
for( int index = 0; index < count; ++index )
{
// compare key
int key = first.keyAt(index);
if( key != second.keyAt(index))
{
return false;
}
// compare value
int value = first.valueAt(index);
if( second.valueAt(index) != value)
{
return false;
}
}
return true;
}
I probably end up getting my own version of the SparseIntArray and overriding the equals method, I think this is cleaner.
[EDIT] Here is the code to implement the equals subclass
import android.util.SparseIntArray;
public class SparseIntArrayComparable extends SparseIntArray {
@Override
public boolean equals( Object obj ) {
if( obj instanceof SparseIntArray ) {
SparseIntArray other = (SparseIntArray)obj;
// compare count
int count = size();
if( count != other.size() )
return false;
// for each pair
for( int index = 0; index < count; ++index ) {
if( keyAt(index) != other.keyAt(index))
return false;
if( valueAt(index) != other.valueAt(index) )
return false;
}
return true;
}
else
return false;
}
}
source to share