This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
why do we need to override to hashcode when the hashcode() of Object class returning unique value for every object??isn't it enough to override equals() method??
thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
It is used to increase the performance of large collections of data by distributing objects evenly accross the buckets. So there wont be a pain of getting objects accumulated into one single bucket.
The hashCode method defined by class Object does return DISTINCT integers for distinct objects.
If you don't override it you don't satisfy the hashCode contract (because different objects can be equal, and in this case their hashCode() methods should return the same value), and you are not able to locate an object in the hashtable, unless you maintain a reference to the original object. [ September 26, 2008: Message edited by: M. Piva ]
SCJP 5.0
Santhi Bharath
Ranch Hand
Joined: Jun 03, 2008
Posts: 75
posted
0
if you overridden equals method then , i guess no problem in not overriding the hashcode().because for every object the Object class returning unique hashcode.then we will have single bucket for every object.
vipin raimcs
Greenhorn
Joined: Sep 26, 2008
Posts: 9
posted
0
This depends upon where you want to use it. Just for nomral purpose its sufficient to override the equal method but sometimes its necessary to override the hashcode method.