Internal Working of HashMap in Java by Raghu
Internal Working of HashMap in Java by Raghu
In this article, we will see how hashmap’s get and put method works
internally. What operations are performed. How the hashing is done.
How the value is fetched by key. How the key-value pair is stored.
HashMap contains an array of Node and Node can represent a class
having following objects :
1. int hash
2. K key
3. V value
4. Node next
Now we will see how this works. First we will see the hashing process.
Hashing
Hashing is a process of converting an object into integer form by using
the method hashCode(). Its necessary to write hashCode() method
properly for better performance of HashMap. Here I am taking key of
my own class so that I can override hashCode() method to show
different scenarios. My Key class is
//custom Key class to override hashCode()
// and equals() method
class Key
{
String key;
Key(String key)
{
this.key = key;
}
@Override
public int hashCode()
{
return (int)key.charAt(0);
}
@Override
public boolean equals(Object obj)
{
return key.equals((String)obj);
}
}
Here overrided hashCode() method returns the first character’s ASCII
value as hash code. So whenever the first character of key is same,
the hash code will be same. You should not approach this criteria in
your program. It is just for demo purpose. As HashMap also allows
null key, so hash code of null will always be 0.
hashCode() method
hashCode() method is used to get the hash Code of an object.
hashCode() method of object class returns the memory reference of
object in integer form. Definition of hashCode() method is public native
hashCode(). It indicates the implementation of hashCode() is native
because there is not any direct method in java to fetch the reference
of object. It is possible to provide your own implementation of
hashCode().
In HashMap, hashCode() is used to calculate the bucket and therefore
calculate the index.
equals() method
equals method is used to check that 2 objects are equal or not. This
method is provided by Object class. You can override this in your
class to provide your own implementation.
HashMap uses equals() to compare the key whether the are equal or
not. If equals() method return true, they are equal otherwise not equal.
Buckets
A bucket is one element of HashMap array. It is used to store nodes.
Two or more nodes can have the same bucket. In that case link list
structure is used to connect the nodes. Buckets are different in
capacity. A relation between bucket and capacity is as follows:
capacity = number of buckets * load factor
A single bucket can have more than one nodes, it depends on
hashCode() method. The better your hashCode() method is, the better
your buckets will be utilized.
Index Calculation in Hashmap
Hash code of key may be large enough to create an array. hash code
generated may be in the range of integer and if we create arrays for
such a range, then it will easily cause outOfMemoryException. So we
generate index to minimize the size of array. Basically following
operation is performed to calculate index.
index = hashCode(key) & (n-1).
where n is number of buckets or the size of array. In our example, I
will consider n as default size that is 16.
Initially Empty hashMap: Here, the hashmap is size is taken as
16.
HashMap map = new HashMap();
HashMap :
class Key {
String key;
Key(String key)
{
this.key = key;
}
@Override
public int hashCode()
{
int hash = (int)key.charAt(0);
System.out.println("hashCode for key: "
+ key + " = " + hash);
return hash;
}
@Override
public boolean equals(Object obj)
{
return key.equals(((Key)obj).key);
}
}
// Driver class
public class GFG {
public static void main(String[] args)
{
HashMap map = new HashMap();
map.put(new Key("vishal"), 20);
map.put(new Key("sachin"), 30);
map.put(new Key("vaibhav"), 40);
System.out.println();
System.out.println("Value for key sachin: " + map.get(new Key("sachin")));
System.out.println("Value for key vaibhav: " + map.get(new
Key("vaibhav")));
}
}
Output:
hashCode for key: vishal = 118
hashCode for key: sachin = 115
hashCode for key: vaibhav = 118