| |
| [ Blog ] [ Open Source ] [ Programming ] | |
« Tweak default Mac OS X keymap to be more Windows/Linux like. | Main | EJB 3.0 Preview for Oracle Application Server. »
I see this mentioned on blogs fairly regularly, and it's always a surprise for people when they learn you have to override hashCode if you override equals.
In short: If you override equals, you must override hashCode.
(From the Javadoc comments from java.lang.Object)
The general contract of hashCode is:
hashCode method on each of
the two objects must produce the same integer result.
equals(java.lang.Object)
method, then calling the hashCode method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hashtables.
Here are a couple of references:
Posted by rcleveng at March 3, 2005 03:09 PM