Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java HashMap collision

Tags:

java

hashmap

I was reading about how hashmap works. I was reading through the "What will happen if two different objects have same hashcode".

According to it if two objects has same hashcode both will be stored in LinkedList but as far as I know if two hashcode then previous one will get overridden with new one(correct me if I am wrong).

Can someone please put more light on how hashmap use object as key internally and what will happen if two objects has same hashcode and how both objects will be fetched with get()?

like image 490
Sandeep Kumar Avatar asked Jan 09 '13 17:01

Sandeep Kumar


People also ask

How does Java HashMap deal with collisions?

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.

How can we avoid HashMap collision in Java?

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.

Does Java HashMap use chaining?

Actually, it does do hash chaining. But chaining is not about storing multiple copies of the same key in a map. It is about dealing with the case where different keys map to the same slot in the hash table.

What happens in a Java HashMap when 2 keys hash to the same location?

It's perfectly legal for two unequal objects to have the same hash code. It's used by HashMap as a "first pass filter" so that the map can quickly find possible entries with the specified key. The keys with the same hash code are then tested for equality with the specified key.


2 Answers

No, the first one isn't overridden just because the second one has the same hashCode.

It will be overridden only if it is also equal (as said by equals). If not, both values will be kept in the linked list.

When fetching a key, all nodes with the same hashCode will be compared to the provided key until one is equal then its value will be returned (using the equals method).

If no key in the map is equal, you'll get null.

The only problem you have if many objects have the same hashCode (or more precisely the same hashCode modulo the size of the internal Entry[] table) is that the linked list will always be read, which is slower (and defeats the purpose of any hash table). That's why it's important when designing a hashcode method to ensure the generated integers are well distributed.

like image 173
Denys Séguret Avatar answered Oct 06 '22 08:10

Denys Séguret


Let me explain working of hashmap.

Working of put method:

HashMap works on principle of hashing, we have put() and get() method for storing and retrieving object form HashMap. When we pass an both key and value to put() method to store on HashMap , it uses key object hashcode() method to calculate hashcode and they by applying hashing on that hashcode it identifies bucket location for storing value object. While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list. Also HashMap stores both key+value tuple in every node of linked list

Working of get method:

When we pass Key and Value object to put() method on Java HashMap, HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object, important point to mention is that HashMap in Java stores both key and value object as Map.Entry in bucket. If more than one Entry object found in the bucket the it will call ke.equals method of each node in same bucket.

like image 20
Rais Alam Avatar answered Oct 06 '22 08:10

Rais Alam