Hashing in
HashMap & HashSet
Discover how Java transforms objects into bucket indices, why hashCode() and equals() are inseparable, and what happens when collisions occur beneath the surface.
What Is Hashing?
Hashing is the process of converting any object into a fixed-size integer called a hash code. Java's hashCode() method produces this integer, and the collection uses it to decide which bucket the object lands in.
The hashCode Contract
Java's documentation mandates a strict contract between hashCode() and equals(). Violating it causes silent, hard-to-debug failures in every hash-based collection.
Inside a HashMap: Buckets
Computing the Bucket Index
Java's HashMap does not use modulo. Instead, it uses a bitwise AND with a power-of-two table size for speed:
What Happens During a Collision?
When two different keys produce the same bucket index, Java stores both entries in that bucket using a linked list. On lookup, it walks the chain, calling equals() on each key until it finds a match.
HashSet Is Just a HashMap
A HashSet doesn't have its own hashing logic. Internally, it delegates everything to a private HashMap where each element is a key and the value is a shared dummy object.
hashCode vs equals
| hashCode() | equals() | |
|---|---|---|
| Purpose | Find the bucket | Confirm identity within bucket |
| Return type | int | boolean |
| Speed | Very fast (arithmetic) | Slower (field-by-field comparison) |
| When called | First: to locate bucket | Second: to match within bucket |
| Can collide? | Yes (expected) | No (must be precise) |
| Default (Object) | Memory address based | Reference equality (==) |
Load Factor & Resizing
The load factor controls when the internal array doubles in size. Java's default is 0.75: when the number of entries exceeds 75% of the table's capacity, the table rehashes all entries into a new, larger array.
How to Override Correctly
Example 1: Correct Override
Example 2: Broken Contract
Example 3: The Mutable Key Trap
Common Mistakes
Summary at a Glance
| Concept | Key Detail | Why It Matters |
|---|---|---|
| hashCode() | Returns int, picks bucket | Wrong bucket = lost entry |
| equals() | Returns boolean, confirms match | Incorrectly returns false = allows duplicate keys |
| Contract | equal \(\to\) same hashCode | Broken contract = silent bugs |
| Collision | Same bucket, different keys | Too many = \(O(n)\) degradation |
| Load factor | Default 0.75, triggers resize | Rehashing is expensive |
| Immutability | Keys must not change after put | Mutation orphans entries |
Question 1 of 5
Two objects a and b satisfy a.equals(b) == true. According to the hashCode contract, which statement must be true?
Question 2 of 5
A class overrides equals() based on an id field but does not override hashCode(). What happens when two logically equal objects are added to a HashSet?
Question 3 of 5
A HashMap has a table length of 16. An object's hashCode() returns 35. Using Java's index formula \( \text{hashCode} \mathbin{\&} (\text{length} - 1) \), which bucket does the object go to?
Question 4 of 5
You use an ArrayList as a key in a HashMap, insert an entry, then add an element to that list. What is the result of calling map.get(key)?
Question 5 of 5
A class overrides hashCode() to always return 1. The code compiles and the contract is technically not violated. What is the practical consequence?