If you only implement the .hashcode() method -- you'll be able to insert objects into a HashTable.
BUT, if you don't implement the .equals() method -- you won't be able to get objects out of a HashTable.
Check out this example from the
Programming Puzzler's session at JavaOne 2002 (Josh Bloch and Neal Gafter).
Look at this code, what's the result?
So, what does it print?
(a) true
(b) false
(c) It varies
----------------------------------------
It varies --> Donald is in the set, but the set can’t find him
The Name class violates the hashCode contract
How do you fix it?
Add a hashCode method:
The moral of the story:
Override hashCode when overriding equals Obey general contracts when overriding See Effective JavaTM, Chapter 3
[ July 01, 2004: Message edited by: Jessica Sant ]