HashMap

Mechanism

mechanism in HashMap to store this key value pair. HashMap has an inner class Entry
static class Entry<K ,V> implements Map.Entry<K ,V>
{
final K key;
V value;
Entry<K ,V> next;
final int hash;
…//More code goes here
}

What put() method actually does

1)    First of all, key object is checked for null. If key is null, value is stored in table[0] position. Because hash code for null is always 0.
2)    Then on next step, a hash value is calculated using key’s hash code by calling its hashCode() method. JDK designers well assumed that there might be some poorly written hashCode() functions that can return very high or low hash code value. To solve this issue, they introduced another hash() function, and passed the object’s hash code to this hash() function to bring hash value in range of array index size.
3)    Now indexFor(hash, table.length) function is called to calculate exact index position for storing the Entry object.
4)    Entry objects are stored in LinkedList form. HashMap checks whether there is already an entry?? If there is no entry already present, Entry object is stored in this location.
5)    If there is already an object sitting on calculated index, its next attribute is checked. If it is null, and current Entry object becomes next node in LinkedList. If next variable is not null, procedure is followed until next is evaluated as null.

What if we add the another value object with same key as entered before. Logically, it should replace the old value. How it is done? Well, after determining the index position of Entry object, while iterating over LinkedList on calculated index, HashMap calls equals method on key object for each Entry object. All these Entry objects in LinkedList will have similar hash code but equals() method will test for true equality. If key.equals(k) will be true then both keys are treated as same key object. This will cause the replacing of value object inside Entry object only.
In this way, HashMap ensure the uniqueness of keys.

How get() methods works internally
Answer we already should know that the way key uniqueness is determined in put() method , same logic is applied in get() method also. The moment HashMap identify exact match for the key object passed as argument, it simply returns the value object stored in current Entry object.
If no match is found, get() method returns null.
Above code is same as put() method till if (e.hash == hash && ((k = e.key) == key || key.equals(k))), after this simply value object is returned.

Key Notes
1.    Data structure to store Entry objects is an array named table of type Entry.
2.    A particular index location in array is referred as bucket, because it can hold the first element of a LinkedList of Entry objects.
3.    Key object’s hashCode() is required to calculate the index location of Entry object.
4.    Key object’s equals() method is used to maintain uniqueness of Keys in map.
5.    Value object’s hashCode() and equals() method are not used in HashMap’s get() and put() methods.
6.    Hash code for null keys is always zero, and such Entry object is always stored in zero index in Entry[].

How will you measure the performance of HashMap?

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.

The capacity is the number of buckets in the hash table( HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.), and the initial capacity is simply the capacity at the time the hash table is created.

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
In HashMap class, the default value of load factor is (.75) .

Leave a comment