HashMap equals hashCode
29 interview questions and answers in the HashMap equals hashCode section.
Questions in this section
- How HashMap Works Internally?
- What is a Bucket in HashMap?
- How HashMap Determines Which Bucket to Put an Element In?
- What is a Collision in HashMap?
- How HashMap Handles Collisions?
- What Happens When 8 Elements Are Reached in One Bucket?
- What is the equals() and hashCode() Contract?
- If Two Objects Are Equal by equals(), What Can You Say About Their hashCode()?
- If Two Objects Have the Same hashCode(), Are They Necessarily Equal by equals()?
- What Happens If You Override equals() But Not hashCode()?
- What Happens If You Override hashCode() But Not equals()?
- Can You Use a Mutable Object as a Key in HashMap?
- What Happens If You Modify a Key After Adding It to HashMap?
- What Are the Requirements for a HashMap Key?
- Why String Is Often Used as a Key in HashMap?
- What is Load Factor in HashMap?
- What is Capacity in HashMap?
- When Does Rehashing (Resize) Occur in HashMap?
- What Happens During Rehashing (Resize)?
- What is the Time Complexity of get() and put() Operations in HashMap?
- When Can Time Complexity Become O(n)?
- How HashMap Works in a Multi-threaded Environment?
- What is ConcurrentHashMap and How Does It Differ from HashMap?
- Can HashMap Store a null Key?
- Can HashMap Store a null Value?
- What is the Difference Between HashMap and Hashtable?
- What is WeakHashMap?
- How to Choose the Initial Capacity for HashMap?
- Which Map Implementation to Choose? (Comparison)
Study navigator
29 questions for Middle Java Developer interview preparation.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β 1. HashMap Internals (Overview) β
β structure, put/get, treeification β
ββββ¬βββββββββββ¬βββββββββββ¬ββββββββββββββββββ
β β β
ββββββββββββ β ββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β BUCKET & β β COLLISIONS β β CONTRACT β
β INDEXING β β (4-6) β β equals/hashCode β
β (2-3) β β 4. What is β β (7-11) β
β 2. Bucket β β collision β β 7. Contract β
β 3. Indexing β β 5. Handling β β 8. equalsβhashCode β
β β β collisions β β 9. hashCodeβequals β
β β β 6. 8 elements β β 10. equals without hashCode
β β β β treeify β β 11. hashCode without equals
βββββββββ¬βββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
βββββββββββββββββββββββΌββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β KEY PROBLEMS (12-15) β
β 12. Mutable key β
β 13. Modification after addition β
β 14. Key requirements β
β 15. Why String is the best key β
ββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββ
β PARAMETERS & RESIZE (16-21) β
β 16. Load Factor β
β 17. Capacity β
β 18-19. Rehashing β
β 20-21. Big O & degradation β
ββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββ
β MULTI-THREADING & NULL (22-27) β
β 22. HashMap in multi-threaded env β
β 23. ConcurrentHashMap β
β 24-25. null keys/values β
β 26. HashMap vs Hashtable β
β 27. WeakHashMap β
ββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββ
β PRACTICE (28-29) β
β 28. Choosing capacity β
β 29. Which Map to choose β
ββββββββββββββββββββββββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | equals/hashCode contract | Q7, Q8, Q9 | Reflexivity, symmetry, transitivity |
| 2 | HashMap structure | Q1, Q2, Q3 | Hash table, buckets, indexing |
| 3 | Collisions | Q4, Q5 | What they are, how theyβre handled |
| 4 | equals/hashCode errors | Q10, Q11 | What happens if you override only one |
| 5 | null keys/values | Q24, Q25 | Why HashMap allows them, Hashtable doesnβt |
| 6 | HashMap vs Hashtable | Q26 | Legacy vs modern approach |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Treeification | Q6 | 8 elements, capacity >= 64, red-black tree |
| 2 | Mutable keys | Q12, Q13 | Why not, what happens |
| 3 | Key requirements | Q14, Q15 | Immutable, hashCode contract, String, Enum |
| 4 | Load Factor and Capacity | Q16, Q17, Q28 | Trade-offs, calculation formula |
| 5 | Rehashing | Q18, Q19 | When it occurs, resize algorithm |
| 6 | Big O | Q20, Q21 | O(1) β O(log n) β O(n), when it degrades |
| 7 | HashMap in multi-threaded env | Q22 | Race conditions, corruption |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | HashMap internals | Q1 (Senior) | spread function, XOR, treeification, churn |
| 2 | Rehashing internals | Q19 (Senior) | Lo/Hi split, one bit, transfer |
| 3 | ConcurrentHashMap | Q23 | CAS, CounterCell, ForwardingNode, Weakly Consistent |
| 4 | WeakHashMap | Q27 | WeakReference, ReferenceQueue, GC, real use cases |
| 5 | O(n) degradation | Q21 (Senior) | Hash Flooding Attack, capacity < 64 |
| 6 | Multi-threading | Q22 (Senior) | safe publication, CAS vs volatile, infinite loop Java 7 |
| 7 | Map comparison | Q29 | Memory footprint, trade-offs, when to choose what |
π Key Topic Connections
Topic: equals/hashCode Contract
Q7 (Contract) β Q8 (equalsβhashCode) β Q9 (hashCodeβ equals)
β β
Q10 (equals without hashCode) Q11 (hashCode without equals)
Key connections:
- Q7 β Q14: Key requirements = extended equals/hashCode contract
- Q10 β Q12-Q13: Contract violation = problems with mutable keys
- Q8 β Q15: String as ideal key β immutable + cached hashCode
Topic: HashMap Internals
Q1 (Structure) β Q2 (Bucket) β Q3 (Indexing)
β β β
Q4 (Collision) β Q5 (Handling) β Q6 (Treeification)
β
Q18-19 (Rehashing)
Key connections:
- Q3 β Q4:
(n-1) & hashβ cause of index collisions - Q5 β Q6: Collision handling β treeification at 8 elements
- Q6 β Q18: At capacity < 64 β resize instead of treeification
- Q19 β Q22: Resize β main point of corruption in multi-threaded env
Topic: Parameters and Performance
Q16 (Load Factor) β Q17 (Capacity) β Q18 (Rehashing)
β β
Q20 (Big O) βββββββββββββββββββββββ Q19 (Resize internals)
β
Q21 (O(n) degradation)
Key connections:
- Q16 β Q17: Load Factor Γ Capacity = resize threshold
- Q17 β Q28: Formula
capacity = expectedSize / loadFactor + 1 - Q18 β Q19: When resize β how resize (Lo/Hi split)
- Q20 β Q21: Normal O(1) β degradation to O(log n) or O(n)
Topic: Multi-threading and Special Maps
Q22 (HashMap in MT) β Q23 (ConcurrentHashMap)
β β
Q24-25 (null) Q26 (vs Hashtable)
β β
Q27 (WeakHashMap) ββββ Q29 (Which is better)
Key connections:
- Q22 β Q23: HashMap problems β ConcurrentHashMap solutions
- Q24-25 β Q23: CHM forbids null β why it matters for thread safety
- Q26 β Q23: Hashtable (legacy, synchronized) β CHM (modern, CAS)
- Q27 β Q12: WeakHashMap = mutable keys under GC control
π Cheat Sheet: What to Know for Each Level
π’ Junior
- HashMap = hash table: array of buckets, each bucket = linked list
- put/get: hashCode() β spread β
(n-1) & hashβ bucket index - equals() and hashCode() β MUST be overridden together
- Equal objects β same hashCode. Same hashCode β equal objects
- HashMap allows null key (one) and null values
- Hashtable β legacy, donβt use it
π‘ Middle
- Collision β normal, not a bug. HashMap handles via linked list
- Treeification (Java 8+): at 8 elements in bucket and capacity >= 64 β red-black tree O(log n)
- Load Factor 0.75 β trade-off between memory and collisions
- Capacity is always a power of two. Formula:
expectedSize / 0.75 + 1 - Rehashing = doubling the array, Lo/Hi split by one bit of hash
- Mutable key = bug: after modification hashCode searches in wrong bucket
- String β ideal key: immutable + cached hashCode
π΄ Senior
- spread function: XOR of high and low 16 bits for uniform distribution
- Resize in Java 8+: Tail Insertion (prevents infinite loop from Java 7), but HashMap still NOT thread-safe
- ConcurrentHashMap: CAS + CounterCell (O(1) size), ForwardingNode during resize, Weakly Consistent iterator
- WeakHashMap: keys = WeakReference, removed by GC. NOT for caches!
- Hash Flooding Attack: attacker creates collisions β O(n). Solution: hash randomization
- Memory: HashMap ~48MB per 1M entries, ConcurrentHashMap ~56MB, TreeMap ~80MB
- At capacity < 64 with collisions β resize instead of treeification β O(n) preserved
π File Format
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, common mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key points, common questions, red flags, related topics