What happens when a duplicate key is put into a HashMap?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java, a HashMap is a Map based collection class that is used for storing key-value pairs. It operates based on the hash table principle. Understanding how HashMap handles duplicates is critical for avoiding common bugs and for effectively utilizing the collection according to its design.
Understanding HashMap
HashMap in Java can have only unique keys but can have duplicate values. That means each key can map to precisely one value. The HashMap class implements the Map interface by using a hash table. Keys are unique, and each key maps to exactly one value. The efficiency of mapping depends on the hash function being used. Ideally, the hash function should distribute keys uniformly across the buckets for better performance.
Handling Duplicate Keys
When you insert a key-value pair into a HashMap, the HashMap calculates the hash code of the key and determines where to place the pair based on this hash code. If you insert a new key-value pair with a key that is not already in the map, it simply finds an empty bucket and adds the new entry.
However, when you try to input a duplicate key, that is, a key which already exists in the HashMap, it does not create a new entry. Instead, the old value associated with that key is replaced by the new value. This mechanism ensures the uniqueness of keys in the map.
Example:
Consider a HashMap where the keys are strings and the values are integers. Here’s a small demonstration:
In the code above, when we put ("A", 3) into the HashMap, it replaces the old value 1 associated with key "A" with 3. The method put returns the previous value associated with the key, or null if there was no mapping for the key.
Collision Handling
HashMap handles collisions (where different keys have the same hash code) using a linked list for chaining. Each bucket can store multiple entries, linked in a list format. With the introduction of Java 8, this mechanism was improved by converting the linked list to a balanced tree when the list's length exceeds a certain threshold. This optimization helps maintain the performance of the HashMap even in cases of high hash collisions.
Summary Table
| Key Feature | Details |
| Handling of Duplicate Keys | Replaces old value with the new value |
| Collision Handling | Uses linked lists, converted to balanced trees in dense buckets |
| Performance | Best case: O(1), Worst case: O(log n) due to tree formation in Java 8+ |
| Key Uniqueness | Only unique keys allowed |
| Value Duplicates | Allows duplicate values |
Return Value of put method | Returns previous value for the key, or null if none |
Best Practices
Understanding how HashMap operates with duplicate keys is vital for efficiently managing key-value pairs in Java. It is crucial to ensure that you handle the returned values of put method correctly to avoid overlooking valuable feedback about previous values, especially when old values are relevant to your application logic. Additionally, careful selection of keys and a good understanding of hash functions can help avoid undesired collisions and maintain the performance of the HashMap.
Conclusion
In conclusion, HashMap provides a very efficient method of handling key-value pairs in Java. The replacement of entries when a duplicate key is inserted offers a straightforward approach to updating values. Proper understanding and good practices with handling hash functions and collisions can leverage the HashMap performance for effective data management and retrieval operations.

