On very high level, Hash Map is used to store data in form of key value pair.
“Hash Map is a Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.” … from Java API
After reading this definition, some question comes in my mind:
- How hash map store data internally?
- What happen when I try to store some new information in map?
- How hash map find my data?
And when I tried to explore it, I find it more and more interesting.
HashMap has a static class named Entry which implements Map.Entry interface. The Entry class looks like:
static class Entry implements Map.Entry {
Object value;
final int hash;
Entry next;
next = entry;
key = obj;
hash = i;
}
// Other methods
}
Every time we insert a <key,value> into hashmap using .put() method, a new Entry object is created (not true is some cases. if key already exists, then it just replace the value). Map internally used two data structures to manage/store data:
- Array
- Link List
This image shows how hashmap manage data. Here
- Each index of array is a bucket
- To identify the bucket for any <key,value>, Hash map use key.hashCode() and perform some operation:
Bucket (index) =HashMap.indexFor (HashMap.hash(key.hashCode()), entryArray.length)
It means, two keys with different hashCode can fall under same bucket. - If a bucket is empty (table[i] is null) then the Entry object is simply inserted at ith position
table[i] = new Entry(hash, key, value, null) - If a bucket has some values, then the following algo is used:
Entry entry = table[i]Table[i] = new Entry(hash,key,value,entry)
It means, the latest entry resides on the top of the bucket.
- If a key already exists in a map, then it just replace the value and return the old value
If(entry.key == key || key.equals(entry.key)
ThenObject oldValue = entry.value;
Entry.value = newValue;
Return oldValue;End if
- In case of new key, map add the key, value (a new Entry object with key value) and return null
- Entry.hash is not the hashCode of key, but HashMap use its own hash algo to create hash based on key.hashCode(). So it’s like:
entry.hash = HashMap.hash(key.hashCode()); - HashMap allow single null as key, in case of null key, it create a dummy object and use it as key.
static final Object NULL_KEY = new Object(); - hashMap.get(Obj) and hashMap.containsKey(obj), both method iterate through the map. Avoid to use extra call of containsKey().
If(null != map.containsKey(obj)) {
myObj = map.get(obj);
//Perform other operations}
This can be easily replaced by:
myMap = map.get(obj);
if(null != myMap) {// Perform other operations}


Hi,
Thanks for this Nice article just to add while discussing about HashMap its worth mentioning following questions which frequently asked in Java interviews now days like How HashMap works in Java or How get() method of HashMap works in JAVA very often. on concept point of view these questions are great and expose the candidate if doesn’t know deep details.
Javin
FIX Protocol tutorial
@Javin,
What use is there in exposing a candidate who doesnt know the details of hash map? Who really cares when they know when and where to use it. Sounds like self righteous bs to me
load factor is another important feature that optimize the performance of a HashMap
visit http://beetechdevzone.blogspot.com/ for more information regarding how load factor effects to the performance
i would say its realy a good article. i faced this question today in an interview.
Its really good article with well designed for new readers to understand in depth.
I would also like to recommend this article
http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html
Can u provide the realistic example of how the key value pairs are arranged in the image given by u above. That would help me better understanding.
this is very useful . me too faced this question in today interview
Thanks for nice posting …………………….
This was excellent article which is giving a clear understanding of the working of Hashmap
Thank for nice article with example, it helped me to understand hashmap datastructure implementation
good one
.. thanks
Thanks
its nice
Nice aricle! Found one more site which talks about hashmap and other maps.
http://javaopensourcecode.blogspot.com/2012/06/hashmap.html
Probably for understanding internals of HashMap, one need to understand the way, hashCode() and equals() methods works. Have a deep insight in this post:
http://howtodoinjava.com/2012/10/09/working-with-hashcode-and-equals-methods-in-java/
nice article
Thanks !
Excellent…explanation….nice…Master…Keep it up…Got lot of confident…