Friday, 3 July 2015

Custom Java HashMap - Internal Working

HashMap is a Collection class in Java that stores objects with the help of hascode and equals. It is said to be faster in retrieving objects.
Lets create a custom implementation of HashMap  and understand how it works. To understand HashMap you need to first understand Singly and DoublyLinkedList.

HashMap stores objects in form of Key and Value. A value can be fetched by passing the Key.

HashMap stores objects into different buckets(LinkedList). The object with same hashcode are stored in same LinkedList. Remember that the objects are stored in key-value pairs. The hashcode of key is used and not the hashcode of value. Only Key alone is required to put and get the object. Value is just a object placed against the key, So depending upon how many objects(Keys) are present with different hashcodes, that many LinkedList gets created.

Share the post