Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Nitin Chopra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I defined an equals method, but Hashtable ignores it. Why?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!