aspose file tools*
The moose likes Java in General and the fly likes Override hashCode  method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Override hashCode  method" Watch "Override hashCode  method" New topic
Author

Override hashCode method

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
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?


I am slow but sure
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Override hashCode method
 
Similar Threads
TreeSet and HashSet
HashSet and LinkrdHashSet
Collections,HashSet
Compiling errata for K&B, SCJP 6
help understanding .equals.