wood burning stoves 2.0*
The moose likes Java in General and the fly likes equals() and hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals() and hashCode() " Watch "equals() and hashCode() " New topic
Author

equals() and hashCode()

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
What is the difference between equals() and hashCode() methods of the Object class?
When do we need to use hashCode() method ?
O. Subramaniam
Greenhorn

Joined: Aug 04, 2003
Posts: 17
hi,
equals() method is used to compare two objects.
i think hashCode() method will return the unique
key [ im not sure about hascode]
regards
o. subramaniam
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
If objectA.equals(objectB) returns true, objectA.hashCode() must equal objectB.hashCode(). Use equals() when you want to test equality because hashCode() is not gaurenteed to be unique for every value, because it's clearer, and because it will perform better.
The hashCode() method exists to be used by data structures such as HashMap. A HashMap will allow very quick insertion and querying of entries, but it relies on the assumption that the values of hashCode() are reasonably varied. Look up hashtables for more information about the specifics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() and hashCode()
 
Similar Threads
what is hashcode and equals contract?
HashSet ordering
map method containsKey
equals - hashcode
On Sets