• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
Popeye has his spinach. I have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic