Understanding Hash Collisions in Java’s HashMap
A hash collision occurs in a hash-based data structure (like HashMap) when two different keys produce the same hash code and therefore are mapped to the same index (or bucket) in the hash table. Since a HashMap uses the hash code to determine where to store entries, a collision happens when multiple keys end up in the same bucket due to their hash codes being identical, even though the keys themselves are different.
Why Collisions Occur:
- Hash Function Limitations: The hash function (
hashCode()in Java) generates a fixed-size integer value for a key, but there are an infinite number of possible keys. This can lead to different keys having the same hash code. - Modulo Operation: The size of the array (buckets) is usually smaller than the number of possible hash codes. The
HashMapuses the modulo operation (index = hash % capacity) to map a hash code to an index, which can result in multiple keys sharing the same index.
Example of a Hash Collision
Let’s take two different strings, “John” and “Doe”, that generate the same hash code.
String key1 = "John";
String key2 = "Doe";
// Assume both keys produce the same hash code (for simplicity, let’s assume they produce 1234).
int hash1 = key1.hashCode(); // returns 1234
int hash2 = key2.hashCode(); // returns 1234
// Suppose the HashMap has an internal array size (capacity) of 16.
// The bucket index is calculated using modulo operation: hash % capacity
int index1 = hash1 % 16; // bucket index for "John" is 1234 % 16 = 2
int index2 = hash2 % 16; // bucket index for "Doe" is 1234 % 16 = 2Even though “John” and “Doe” are different strings, they both have the same bucket index (2), causing a hash collision.
Handling Hash Collisions in HashMap
When a collision occurs, HashMap uses separate chaining (linked list or binary tree) to handle the situation. Multiple key-value pairs can be stored in the same bucket, but they are chained together using a linked list or tree structure.
Example :
HashMap<String, Integer> map = new HashMap<>();
map.put("John", 25); // Stored in bucket 2 (assume hash % 16 = 2)
map.put("Doe", 30); // Also stored in bucket 2 (same hash collision)Internal Representation :
Bucket 0 : null
Bucket 1 : null
Bucket 2 : ("John" -> 25) -> ("Doe" -> 30) // Collision handled via linked list
Bucket 3 : null
... (other buckets)- First Insertion (“John”, 25): The hash code for “John” is calculated and the entry is placed in bucket 2.
- Second Insertion (“Doe”, 30): When “Doe” is inserted, a collision is detected since it also maps to bucket 2. Instead of overwriting,
HashMapadds the new key-value pair to the end of the linked list in bucket 2.
Retrieval in Case of a Hash Collision
If you call get() on a key, HashMap will:
- Compute the hash code of the key.
- Calculate the bucket index.
- Search through the linked list (or binary tree) in that bucket to find the correct key-value pair.
int value1 = map.get("John"); // Returns 25
int value2 = map.get("Doe"); // Returns 30- For
get("John"), it checks bucket 2 and finds "John" in the linked list, returning the value25. - For
get("Doe"), it continues through the linked list in bucket 2 until it finds "Doe" and returns the value30.






