• Post Reply Bookmark Topic Watch Topic
  • New Topic

How HashMap works and how keys and values are linked internally in HashMap ?  RSS feed

 
Prakash Pasumarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me how Hashing works in HashMap and
how keys and values are linked internally in HashMap ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, in java the base datastructure for a HashMap is Array also it uses linkedlist when there are collisions. it is difficult to explain in detail. you can go through source code. hope you understand what is hashing...
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keys and values are linked as entry pairs by being put into Map.Entry objects. The hash code of the key tells the JVM which location in the array to put the entry pair. If there are two entries whose keys have the same hash code, they are distinguished by seeing whether they return true from the equals() method.
The size of the array is always 2, 4, 8, 16, 32, 64, etc, so you can use the lowest however many bits of the hash code to determine the array index. In most cases, the array starts off at 16 (or larger) and doubles its size as it fills.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!