• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HashMap entries with duplicate keys...

 
Greenhorn
Posts: 6
jQuery Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi, In spite of overriding equals, why HashMap is allowing duplicate entries...?




Thanks...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello J World wrote:
Hi, In spite of overriding equals, why HashMap is allowing duplicate entries...?



you should override hashCode method too. because HashMap entry checks the equality of both hashCode and equals . if any one fails then HashMap add the new entry value. @see HashMap#put(K, V)

<edit> also your overridden equals method can be fragile . have a look how to override an equals method</edit>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello J World ?

and welcome to JavaRanch .
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

Hello J World wrote:
Hi, In spite of overriding equals, why HashMap is allowing duplicate entries...?



you should override hashCode method too. because HashMap entry checks the equality of both hashCode and equals . if any one fails then HashMap add the new entry value. @see HashMap#put(K, V)

<edit> also your overridden equals method can be fragile . have a look how to override an equals method</edit>



yaa..I know i must apply instatnceof check as well...but
What about the default implementation of hashcode...?
As per my understanding it puts all the elements in a single bucket...doesn't it...?


Thanks...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jigar Gohil wrote:
What about the default implementation of hashcode...?



@see java.lang.Object#hashCode
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it doesn't. It's more likely to put all objects in different buckets. But you really should read the Javadoc comments of Object.equals and Object.hashCode. They explain you exactly which rules you must follow if you override one or both.
 
Goodbye moon men. Hello tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic