aspose file tools*
The moose likes Java in General and the fly likes I defined an equals method, but Hashtable ignores it. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I defined an equals method, but Hashtable ignores it. Why?" Watch "I defined an equals method, but Hashtable ignores it. Why?" New topic
Author

I defined an equals method, but Hashtable ignores it. Why?

Nitin Chopra
Greenhorn

Joined: Oct 25, 2007
Posts: 1
I defined an equals method, but Hashtable ignores it. Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Nitin Chopra:
I defined an equals method, but Hashtable ignores it. Why?



Hashtable uses both the hashCode() and equals() methods. It only calls to the equals() method to confirm that two objects are equal, if they hash to the same bucket.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
hi nitin

If you override equals() alone,then each object gets a unique hashcode generated by the hashcode() declared in Object class.

The contract is if two objects have same hashcode alone ,it will check equals() for equality.

Since all objects will now be having unique hascode,your equals() is of no help.

Hope this helps.

Praveen SP
 
 
subject: I defined an equals method, but Hashtable ignores it. Why?