• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

HashMap entries with duplicate keys...

 
Greenhorn
Posts: 6
jQuery Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hello J World ?

and welcome to JavaRanch .
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

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



@see java.lang.Object#hashCode
 
Sheriff
Posts: 21783
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!