UrbanPro
true

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

R
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

Submit

Other Lessons for You


A Tip for the beginners.
The world of programming languages right now is too dense, and too big for any beginner. But at the same time, because of so many options available, it's easier too. You can start with any language that...

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S

Priority in TestNG
public class Priority { @Test (priority=1)public void login() {System.out.println("login");} @Testpublic void email1() {System.out.println("email1");} @Test (priority=-2)public void email2() {System.out.println("email2");} //I...
S

Sarthak C.

0 0
0

Java: A Quick Overview
Not purely Object Oriented: It doesn't support multiple inheritence, it supports primitive data types and static members. Doesn’t support multiple inheritance: Reason is diamond problem i.e.,...
X

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