Equality between 2 HashMap
Native Java arrays don't have a .equals() function. So if your hashmap's values (or keys I suppose) are arrays, HashMap.equals() will fail. I suspect it'd fall back on Object.equals() which just checks to see if the two objects are actually the same object.
// something like this
class Object {
public boolean equals( Object o) {
return this == o;
}
}
You can sidestep the problem by using some variant on a Container rather than an array[], as containers have their own .equals() which calls equals() on successive elements of the containers rather than simply checking if they're the same reference. The code for a Collection.equals implementation might look something like:
public boolean equals(Object o) {
// sets never equal lists and visa versa
if (o instanceof MyCollectionSubclass) {
Iterator myIterator = iterator();
Iterator theirIterator = ((Collection)o).iterator();
while (myIterator.hasNext() && theirIterator.hasNext()) {
Object myObj = myIterator.next();
Object theirObj = theirIterator.next();
if (!myObj.equals(theirObj)) {
return false;
}
}
// at least one will be false or we wouldn't have left the above while loop
return myIterator.hasNext() == theirIterator.hasNext();
}
// not our class
return false;
}
This might produce a true value comparison depending on what the collection's contents do when you call their equals()
.
The equals
method on a Java array type is equivalent to ==
, because Java array "classes" do not override Object.equals
.
If you want to compare arrays "by value" you need to either use the appropriate java.util.Arrays.equals(...)
method, or implement it yourself.
If your HashMap
uses arrays as keys or values, then it is going to call the array's equals
method to test if the keys and/or values are the same between two maps. This would make HashMap.equals
behave strangely (from your perspective). That is what the linked article is saying. However, array semantic only affect HashMap
equality if you use arrays as the key or value classes. If you don't, then HashMap::equals
should just work as expected.
The javadocs for equality on Map
classes are a bit involved, but they basically boil down to taking the two entry sets, comparing their sizes, and then doing s1.containsAll(s2)
. Of course, this is expensive, but it should work for all of the Map
classes that correctly implement the Map
interface.
Note that using arrays as keys for maps is a bad idea for a couple of reasons:
- The semantics of array
equals
andhashCode
are wrong for aHashMap
in most scenarios. For most use-cases, you need the map to compare the keys by value not by object identity. - Arrays are mutable. If we assumed that there was a workaround for the
equals
/hashcode
problem, you could still break a map's invariants by modifying an array key.
The article is right. Hashmaps can be safely compared using the equals() method as long as the key objects and value objects are possible to compare using the same method. In the article, the map values are arrays, which do not implement equals() as expected. Using ArrayList instead would have solved the problem.