| 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: 16695
|
|
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)
|
 |
 |
|
|
subject: Override hashCode method
|
|
|