• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I defined an equals method, but Hashtable ignores it. Why?
 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic