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.
- If not, it stores the {Key,value} pair in that position.
- 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). - 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:
------------------------------------------
- First it checks for null key.
- It will take the hash code of Key and calculates the hash value.
- By using this hash value, it ll get the table index position of that key.
- 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.