• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() and hashCode()

 
thomas davis
Ranch Hand
Posts: 207
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between equals() and hashCode() methods of the Object class?
When do we need to use hashCode() method ?
 
O. Subramaniam
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic