java.util.HashMap
has an implementation of the put method, which has the following code inside it :
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
In the above code why wasn't the reference check made first (since two objects having the same reference will have the same hash and equals()) ?
i.e. something like this :
if ((k = e.key) == key) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
} else if ( compare hash and equals) {
// do something again with the value
}
Wouldn't this have saved a comparision?