File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 18978
    
  40

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Override hashCode method