Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Java Hash Map internal implementation

Ravi Kumar
05/12/2016 0 0

1.Hash Map internal implementation:

In hash map it will create an array of table (to store the {Key,Value} pair) which is of type java.util.HashMap.Entry
The Entry object contains {hash, key, next, value} fields.
Each entry in this table is called as Bucket {Bucket is an implementation of Linked list}.

{Key,Value} --> which is given by developer to store in hash map.
hash is the hash value (which is calculated using the #hashcode of the object).
Next --> is pointer to next element in that bucket .
If the hash map is of size 10, then 10 buckets will be created in the hash map, in each index position.
If the keys are different, then the each <key,value>pair is stored in different index positions of Hash Map.
If the keys are same (Means same hash code but different memory references) at that time if map is trying to store the 2 elements in same index location, it will give collision.
To avoid this collision, java people implemented Bucket concept.

By using bucket concept hash map will store this type of values in the bucket (Liked list)as next values. (Already existed {Key,value} pair will move inside and new one will come into first position in bucket.).

If the keys are same (Means same hash code and same memory references or as per Object.equals method ), at that time hash map simply replaces the existing value with this new value.

Put(Key,value) method implementation:

First it will take the Key, and it checks that the key is null or not, if the key is null it will add into map at first position.
If there is already a null key is existed , then it will replaces the Value with new value,and returns the old value.
If not null it will take the hash code of key ,and calculates the hash value.
By using the hash value and table length, it calculates the position in the table for this current {key,value} pair.
After calculating the position, it checks, is there any element in that position.

  1. If not, it stores the {Key,value} pair in that position.
  2. If there is any element already existed in that position, it will compare the existed element hash with current element hash if it is true, then compares the existed element key reference(Memory) with current element key reference || (or) uses equals method to compare.
    If these two are true. it will replace the current value directly with old value in that key location(Key is same).
  3. If the hash value is same but memory references are different (Hash collision), at that time for already existed <k,v>pair is moved to next position of new <k,v>Pair. in same table index location. The above method is called bucket concept. It is implemented to resolve the Hash collision. Bucket is an instance of Linked List.

Get method implementation:

  1. First it checks for null key.
  2. It will take the hash code of Key and calculates the hash value.
  3. By using this hash value, it ll get the table index position of that key.
  4. In that index position it will take the element and compare the hash value of that element with the key hash value (FYI in one bucket all elements contains same hash value), if it is true it will compare the existed object(In that position) key reference with, the search key reference || (or) search key Object.equals() with the existed object key.
    If both are true. Then it will return Value in that location.
    If both are false and there are some more elements in that bucket,it ll continue the above process and finds the appropriate {key,Value} pair and returns this pair.
0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?
As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program. The answer to this...

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

Finding a Majority Element
Problem Description Task. The goal in this code problem is to check whether an input sequence contains a majority element. Input Format. The first line contains an integer, the next one contains a sequence...

Object Oriented Programming Concepts :
Class : Class is user defined data type. Class is a logical representation of an Object. That means Class is used to define an object, how you want to create your software object. For Example...

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more