This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 16687
    
  19

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I defined an equals method, but Hashtable ignores it. Why?
 
Similar Threads
Reason for Override!
Hashtable key object should have its "equals" method
Binary to Decimal conversion
okay what am i doing wrong?
util package