Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nitin Chopra
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I defined an equals method, but Hashtable ignores it. Why?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Praveen Seluka
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic