my dog learned polymorphism*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Sitaram,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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: http://aspose.com/file-tools
 
subject: why we need to override equals and hascode
 
Similar Threads
Overriding hashCode()
Primary Key Class
hashcode
Overriding equals()
Object hashCode ?