about equals() and hashCode()?

R

rmn190

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.
 
L

Lasse Reichstein Nielsen

rmn190 said:
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
 
P

Patricia Shanahan

rmn190 said:
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
 
L

Lew

Patricia said:
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.

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?
 
W

Wayne

rmn190 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top