Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Question on HashMap internal implementation

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Question on HashMap internal implementation

    HashMap internally uses Array and LinkedList to store the key value pairs. Can someone give detailed view of how exactly they are stored.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Question on HashMap internal implementation

    Did you try google? Don't come up here with questions like, how to do this? How it works? How can i do this?
    As google can explain you this in detail.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on HashMap internal implementation

    Have tried google. Didn't get clear answer for my above qn. Thats the reason for posting the same.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Question on HashMap internal implementation


  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question on HashMap internal implementation

    Start at the source

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on HashMap internal implementation

    Copeg,

    Hashmap.jpg

    W.r.t image attached
    Each node in the linked lists stores a pairing of a key with a value. Now, to look for the mapping for, say, Ireland, we first compute this key's hash code (in this case, the string length, 7). Then we start traversing the linked list at position 7 in the table. We traverse each node in the list, comparing the key stored in that node with Ireland. When we find a match, we return the value from the pair stored in that node (Dublin). In our example here, we find it on the second comparison. So although we have to do some comparisons, if the list at a given position in the table is fairly short, we'll still reduce significantly the amount of work we need to do to find a given key/value mapping.

    Each Linked list node stores a value and link to the next node. My question is if "key" (of hashmap) is stored as value and second portion of node has link to the next node, where the exact object is stored which is paired with key of hashmap?

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on HashMap internal implementation

    You can see How HashMap works in Java an interesting article.

Similar Threads

  1. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  2. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  3. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
  4. Help wih implementation...
    By Frank_nor in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 24th, 2009, 05:43 PM
  5. B+ Tree implementation
    By programmer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2009, 05:47 PM