Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Override hashCode method

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we dont override the hashCode method then how the objects are stored inside HashSet and will all the objects inside HashSet have the same hashCode?
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MaheshS Kumbhar wrote:If we dont override the hashCode method then how the objects are stored inside HashSet and will all the objects inside HashSet have the same hashCode?


If you don't override the hashcode, the hashcode method from the Object class will be used, which returns the identity hashcode -- and if you overridden equals() (but not hashcode), then depending on what you did, you can corrupt the HashSet.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic