*
The moose likes Java in General and the fly likes Hashtable key object should have its Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable key object should have its "equals" method " Watch "Hashtable key object should have its "equals" method " New topic
Author

Hashtable key object should have its "equals" method

javabird user
Greenhorn

Joined: Aug 16, 2011
Posts: 3
Hi All,

Please tell me why an object which is to be used as a key in hashtable should have equals method overridden..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
. . . and its hashCode method.
The hashCode method is used to decide which "bucket" the key-value pair is put into, and the equals() method is used to disambiguate pairs, in case there is more than one pair in a particular "bucket".If the JavaRanchUser class has an overridden equals() method [and hashCode()], that will print out NUISANCE. If the equals() method is not, however, overridden, the Map will not recognise the two keys as identical, and will return something else, and you will get a printout of "null". If the hashCode method is not overridden, the Map will not even find the correct "bucket".
 
wood burning stoves
 
subject: Hashtable key object should have its "equals" method
 
Similar Threads
equals( ) & hashCode( )
hashtable working & overriding equals
hashtable working & overriding equals()
Regarding StringBuffers
Hashtable doesn't allow null value and HashMap allows only one null as key