Section 10 Β· 29 questions

HashMap equals hashCode

29 interview questions and answers in the HashMap equals hashCode section.

English HashMap equals hashCode Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. How HashMap Works Internally?
  2. What is a Bucket in HashMap?
  3. How HashMap Determines Which Bucket to Put an Element In?
  4. What is a Collision in HashMap?
  5. How HashMap Handles Collisions?
  6. What Happens When 8 Elements Are Reached in One Bucket?
  7. What is the equals() and hashCode() Contract?
  8. If Two Objects Are Equal by equals(), What Can You Say About Their hashCode()?
  9. If Two Objects Have the Same hashCode(), Are They Necessarily Equal by equals()?
  10. What Happens If You Override equals() But Not hashCode()?
  11. What Happens If You Override hashCode() But Not equals()?
  12. Can You Use a Mutable Object as a Key in HashMap?
  13. What Happens If You Modify a Key After Adding It to HashMap?
  14. What Are the Requirements for a HashMap Key?
  15. Why String Is Often Used as a Key in HashMap?
  16. What is Load Factor in HashMap?
  17. What is Capacity in HashMap?
  18. When Does Rehashing (Resize) Occur in HashMap?
  19. What Happens During Rehashing (Resize)?
  20. What is the Time Complexity of get() and put() Operations in HashMap?
  21. When Can Time Complexity Become O(n)?
  22. How HashMap Works in a Multi-threaded Environment?
  23. What is ConcurrentHashMap and How Does It Differ from HashMap?
  24. Can HashMap Store a null Key?
  25. Can HashMap Store a null Value?
  26. What is the Difference Between HashMap and Hashtable?
  27. What is WeakHashMap?
  28. How to Choose the Initial Capacity for HashMap?
  29. Which Map Implementation to Choose? (Comparison)

Study navigator

29 questions for Middle Java Developer interview preparation.


πŸ“‹ All Questions

# Question Difficulty
1 How HashMap Works Internally ⭐⭐
2 What is a Bucket in HashMap ⭐
3 How HashMap Determines Which Bucket to Put an Element In ⭐⭐
4 What is a Collision in HashMap ⭐⭐
5 How HashMap Handles Collisions ⭐⭐
6 What Happens When 8 Elements Are Reached in One Bucket ⭐⭐⭐
7 What is the equals() and hashCode() Contract ⭐⭐
8 If Two Objects Are Equal by equals(), What Can You Say About Their hashCode() ⭐
9 If Two Objects Have the Same hashCode(), Are They Necessarily Equal by equals() ⭐⭐
10 What Happens If You Override equals() But Not hashCode() ⭐⭐
11 What Happens If You Override hashCode() But Not equals() ⭐⭐
12 Can You Use a Mutable Object as a Key in HashMap ⭐⭐⭐
13 What Happens If You Modify a Key After Adding It to HashMap ⭐⭐⭐
14 What Are the Requirements for a HashMap Key ⭐⭐
15 Why String Is Often Used as a Key in HashMap ⭐⭐
16 What is Load Factor in HashMap ⭐⭐
17 What is Capacity in HashMap ⭐⭐
18 When Does Rehashing Occur in HashMap ⭐⭐⭐
19 What Happens During Rehashing ⭐⭐⭐
20 What is the Time Complexity of get() and put() Operations in HashMap ⭐⭐
21 When Can Time Complexity Become O(n) ⭐⭐⭐
22 How HashMap Works in a Multi-threaded Environment ⭐⭐⭐
23 What is ConcurrentHashMap and How Does It Differ from HashMap ⭐⭐⭐
24 Can HashMap Store a null Key ⭐⭐
25 Can HashMap Store a null Value ⭐⭐
26 What is the Difference Between HashMap and Hashtable ⭐
27 What is WeakHashMap ⭐⭐⭐
28 How to Choose the Initial Capacity for HashMap ⭐⭐
29 Which Map Implementation to Choose ⭐⭐

πŸ—ΊοΈ 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                β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 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