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

Java : Command Line Arguments
The parameters which are provided to the program at the command line. Eg:d:\>java a 10 20 30 Here,“java” is an interpreter, “a” is filename,10,20,30 are arguments passed to...
S

Svg Reddy

0 0
0

Android : Support multiple themes in an android application
If you are developing a theme based application to enhance user experience, then following steps needs to follow.We are taking example of an android application having 2 themes white and black.Step 1:Define...

Simple Input and Output in CoreJava
We can use any of the following options based on the requirements to accept data from the user in corejava and show them Integer Input Output Character Input Output Float Input Output INPUT AND...

Java inheritance example
Answer these questions Answer the above question

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
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