This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals() and hashCode() " Watch "equals() and hashCode() " New topic

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

Joined: Aug 04, 2003
Posts: 17
equals() method is used to compare two objects.
i think hashCode() method will return the unique
key [ im not sure about hascode]
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.
It is sorta covered in the JavaRanch Style Guide.
subject: equals() and hashCode()
It's not a secret anymore!