File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes why we need to override equals and hascode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why we need to override equals and hascode" Watch "why we need to override equals and hascode" New topic

why we need to override equals and hascode

sitaram irrinki
Ranch Hand

Joined: Feb 16, 2005
Posts: 158
why we need to override methods of object hashcode
and equals in case of Hashtable and RMI
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124

A HashTable (or HashMap in the Collections API) stores data based on a certain value. It calls the hashCode() method to see where the value should be stored. There is also a rule that if you implement hashCode(), you must also implement equals() and vice versa. This is for consistency purposes as objects that are equal must have the same hash code.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Josh Bloch: Effective Java
JavaWorld: Implementing equals and hashCode
Sun's Java Tutorial: The equals and hashCode Methods
IBM: Defining hashCode() and equals() effectively and correctly
JavaPractices: Implementing hashCode
JavaWorld: override hashcode/equals
I agree. Here's the link:
subject: why we need to override equals and hascode
jQuery in Action, 3rd edition