the java API says that:"If two objects are equal according to the
equals(Object) method, then calling the hashCode method on each of the
two objects must produce the same integer result."
why?i wonder the root cause of that.
Lasse Reichstein Nielsen - 16 Dec 2007 12:01 GMT
> the java API says that:"If two objects are equal according to the
> equals(Object) method, then calling the hashCode method on each of the
> two objects must produce the same integer result."
> why?i wonder the root cause of that.
Because hashCode() is used in HashMap and Hashtable to store the
element in a bucket. If two elements are considered equal, then
adding one to a HashMap and then removing the other, should remove
the first one added (because, as far as HashMap knows, they are the
same object).
If they have different hashCode values, then the remove fails to
find the bucket where the first object added is stored, and cannot
remove it.
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Patricia Shanahan - 16 Dec 2007 14:31 GMT
> the java API says that:"If two objects are equal according to the
> equals(Object) method, then calling the hashCode method on each of the
> two objects must produce the same integer result."
> why?i wonder the root cause of that.
Because it is needed to make hashCode useful.
Take a look at the source code for e.g. HashMap. It assigns the keys to
buckets based on their hash codes. During a get call, it calculates the
bucket for the probe key's hash code, and only looks there. If two
objects with different hash codes could be equal, it would have to look
in every bucket.
Patricia
Lew - 16 Dec 2007 19:42 GMT
>> the java API says that:"If two objects are equal according to the
>> equals(Object) method, then calling the hashCode method on each of the
[quoted text clipped - 8 lines]
> objects with different hash codes could be equal, it would have to look
> in every bucket.
Looking at what Patricia said from a different angle - the very purpose of
hashCode() is to speed up determination of equality. If you break the
connection between hashCode() and equals(), you do not have a useful
hashCode(). So what would be the point of breaking it?

Signature
Lew
Wayne - 16 Dec 2007 20:38 GMT
> the java API says that:"If two objects are equal according to the
> equals(Object) method, then calling the hashCode method on each of the
> two objects must produce the same integer result."
> why?i wonder the root cause of that.
When checking a collection to find an element equal to another,
the system uses hashCode to quickly eliminate elements that
can't be equal. It is legal to set hashCode to return
a constant, say 1. If so your collections will exhibit
poorer performance.
Note the inverse is not necessarily true; if the hashCodes
are the same, you still need to check for equality.
Java has contracts like this that are not checked
by a compiler; it is up to the programmer to ensure that
when you over-ride equals, you make sure hashCode is
acceptable (usually when you over-ride one, you over-ride
the other).
-Wayne