• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Key with same hashcode

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

How HashMap stores key if all the added objects has same hashcode(say 1), does it maintain internal subset of Key+value as key, if so what is the data type of subset(is it stored as hashmap inside hashmap)?

Thanks,
Arun
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the source of HashMapyou can see that the entries are in an array of Entry objects. The for-loop just looks at each entry with the same hash until it finds one with a key that matches.
 
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

Arun Chidam wrote:How HashMap stores key if all the added objects has same hashcode(say 1)


you cant talk about adding an object in HashMap with only hashCode.you need to mention equals method also. what about equals method of all objects? if equals method of all objects are same then recent entry will replace existing and so on... else all the objects will go into one single bucket; bad programing style.

Arun Chidam wrote:
does it maintain internal subset of Key+value as key, if so what is the data type of subset(is it stored as hashmap inside hashmap)?


nope. key+value wrapped into an Object called Entry and put into an Array. the base datastructure for the Hashtable/HashMap is an array.
for more details you can view the source code of java.util.HashMap.
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman,Luigi

Your post cleared my doubts

Thanks,
Arun
 
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

Arun Chidam wrote:Thanks Seetharaman


You are Welcome
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic