Java HashMap
uses put
method to insert the K/V pair in HashMap
. Lets say I have used put
method and now HashMap<Integer, Integer>
has one entry with key
as 10 and value
as 17.
If I insert 10,20 in this HashMap
it simply replaces the the previous entry with this entry due to collision because of same key 10.
If the key collides HashMap
replaces the old K/V pair with the new K/V pair.
So my question is when does the HashMap
use Chaining collision resolution technique?
Why it did not form a linkedlist
with key as 10 and value as 17,20?
1) HashMap handles collision by using a linked list to store map entries ended up in same array location or bucket location. 2) From Java 8 onwards, HashMap, ConcurrentHashMap, and LinkedHashMap will use the balanced tree in place of linked list to handle frequently hash collisions.
So for a collision to occur in a HashMap, the necessary and sufficient condition is the following : scramble(k1. hashCode()) == scramble(k2. hashCode()) . This is always true if k1.
The only way to avoid (or rather minimize) collisions is to create a hash function that creates the best possible distribution of values throughout the HashMap. Depending on the density of your HashMap and the quality of your hash code , collisions are almost inevitable, hence the need to override the two methods.
Hash collision is resolved by open addressing with linear probing. Since CodeMonk and Hashing are hashed to the same index i.e. 2, store Hashing at 3 as the interval between successive probes is 1. There are no more than 20 elements in the data set. Hash function will return an integer from 0 to 19.
When you insert the pair (10, 17)
and then (10, 20)
, there is technically no collision involved. You are just replacing the old value with the new value for a given key 10
(since in both cases, 10 is equal to 10 and also the hash code for 10 is always 10).
Collision happens when multiple keys hash to the same bucket. In that case, you need to make sure that you can distinguish between those keys. Chaining collision resolution is one of those techniques which is used for this.
As an example, let's suppose that two strings "abra ka dabra"
and "wave my wand"
yield hash codes 100
and 200
respectively. Assuming the total array size is 10, both of them end up in the same bucket (100 % 10
and 200 % 10
). Chaining ensures that whenever you do map.get( "abra ka dabra" );
, you end up with the correct value associated with the key. In the case of hash map in Java, this is done by using the equals
method.
In a HashMap
the key is an object, that contains hashCode()
and equals(Object)
methods.
When you insert a new entry into the Map, it checks whether the hashCode
is already known. Then, it will iterate through all objects with this hashcode, and test their equality with .equals()
. If an equal object is found, the new value replaces the old one. If not, it will create a new entry in the map.
Usually, talking about maps, you use collision when two objects have the same hashCode
but they are different. They are internally stored in a list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With