Hi,
As a techie one general thing I have learnt is to have good understanding about how HashMap in Java works. This is also often asked in almost all the technical interviews.
So today, let's visit hashMaps in java.
Hashmap is a data structure where the pairs of key and value are stored. The operation on hashmap are insert, delete and retrieve.
For understanding hashmap working, its important to understand that hash map uses a nested class called Entry (Map.Entry<K,V>) for actually storing key, value pair.
So actually entry is the object created to store the key and value. Hashmap is just a wrapper around entry. Hashmap manages the operations performed on the entries.
One more important point to remember Java collections only allow objects to be keys and values. So is for hashmap. Which means you can not have primitive data types as key or value in hashmap.
So lets see how hashmap works in a simple approach:
Lets say we have K [key class] and V [value class]
public class K {
}
public class V {
}
There is default implementation of hashCode and equals in K and V classes inherited from java.lang.Object class.
When we say
K k = new K();
V v = new V();
hashmap.put(k,v)
Hashmap creates a Map.Entry instance with (k,v). hashmap implementation gets k.hashCode() value, figures out which bucket to place this Entry.
The buckets can be imagined as range in array of storage location.
When we say, hashmap.get(k) hashmap will again calls k.hashcode() and then figures out which bucket to look into for getting this entry.
It is general practice that, when you overwrite hashCode method, you should also overwrite equals method.
Please feel free to comment.
To be continued...
Next post - how equals and hashcode play role in creating entry in hashmap
As a techie one general thing I have learnt is to have good understanding about how HashMap in Java works. This is also often asked in almost all the technical interviews.
So today, let's visit hashMaps in java.
Hashmap is a data structure where the pairs of key and value are stored. The operation on hashmap are insert, delete and retrieve.
For understanding hashmap working, its important to understand that hash map uses a nested class called Entry (Map.Entry<K,V>) for actually storing key, value pair.
So actually entry is the object created to store the key and value. Hashmap is just a wrapper around entry. Hashmap manages the operations performed on the entries.
One more important point to remember Java collections only allow objects to be keys and values. So is for hashmap. Which means you can not have primitive data types as key or value in hashmap.
So lets see how hashmap works in a simple approach:
Lets say we have K [key class] and V [value class]
public class K {
}
public class V {
}
There is default implementation of hashCode and equals in K and V classes inherited from java.lang.Object class.
When we say
K k = new K();
V v = new V();
hashmap.put(k,v)
Hashmap creates a Map.Entry instance with (k,v). hashmap implementation gets k.hashCode() value, figures out which bucket to place this Entry.
The buckets can be imagined as range in array of storage location.
When we say, hashmap.get(k) hashmap will again calls k.hashcode() and then figures out which bucket to look into for getting this entry.
It is general practice that, when you overwrite hashCode method, you should also overwrite equals method.
Please feel free to comment.
To be continued...
Next post - how equals and hashcode play role in creating entry in hashmap
No comments:
Post a Comment