🔑 Section 10 · 29 questions

HashMap equals hashCode

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

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 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

Study navigator

29 curated interview questions and deep-dive technical guides for Junior 🟢, Middle 🟡, and Senior 🔴 Java Developers.


📋 All Section Questions

# Question Difficulty Level
01 How HashMap Works Internally ⭐⭐
02 What is a Bucket in HashMap ⭐
03 How HashMap Determines Which Bucket to Put an Element In ⭐⭐
04 What is a Collision in HashMap ⭐⭐
05 How HashMap Handles Collisions ⭐⭐
06 What Happens When 8 Elements Are Reached in One Bucket ⭐⭐⭐
07 What is the equals() and hashCode() Contract ⭐⭐
08 If Two Objects Are Equal by equals(), What Can You Say About Their hashCode() ⭐
09 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

                    ┌──────────────────────────────────────────┐
                    │   01. HashMap Internals (Overview)       │
                    │   table array, put/get, treeification    │
                    └──┬──────────┬──────────┬─────────────────┘
                       │          │          │
            ┌──────────┘          │          └──────────┐
            ▼                     ▼                     ▼
    ┌───────────────┐    ┌───────────────┐    ┌────────────────────┐
    │ BUCKETS &     │    │ COLLISIONS    │    │ CONTRACT           │
    │ INDEXING      │    │ (04–06)       │    │ equals/hashCode    │
    │ (02–03)       │    │ 04. Collision │    │ (07–11)            │
    │ 02. Bucket    │    │ 05. Handling  │    │ 07. Formal Contract│
    │ 03. Indexing  │    │ 06. 8 items   │    │ 08. equals→hashCode│
    │               │    │     → Treeify │    │ 09. hashCode→equals│
    │               │    │               │    │ 10. equals w/o hash│
    │               │    │               │    │ 11. hash w/o equals│
    └───────┬───────┘    └───────┬───────┘    └────────┬───────────┘
            │                     │                     │
            └─────────────────────┼─────────────────────┘
                                  ▼
                    ┌──────────────────────────────────────────┐
                    │   KEY REQUIREMENTS & MUTABILITY (12–15)  │
                    │   12. Mutable keys                       │
                    │   13. Modification after insertion       │
                    │   14. Contract requirements for keys     │
                    │   15. String as an ideal key             │
                    └──────────────────────────────────────────┘

                    ┌──────────────────────────────────────────┐
                    │   CAPACITY, LOAD FACTOR & RESIZE (16–21) │
                    │   16. Load Factor                        │
                    │   17. Capacity                           │
                    │   18-19. Rehashing & Lo/Hi Split         │
                    │   20-21. Big-O Complexity & Degradation  │
                    └──────────────────────────────────────────┘

                    ┌──────────────────────────────────────────┐
                    │   CONCURRENCY, NULL & MAP TYPES (22–27)  │
                    │   22. Multi-threaded race conditions     │
                    │   23. ConcurrentHashMap CAS/Locking      │
                    │   24-25. null key & null values          │
                    │   26. HashMap vs Hashtable               │
                    │   27. WeakHashMap & ReferenceQueue       │
                    └──────────────────────────────────────────┘

                    ┌──────────────────────────────────────────┐
                    │   PRACTICAL SIZING & SELECTION (28–29)   │
                    │   28. Sizing Initial Capacity            │
                    │   29. Map Implementation Decision Matrix │
                    └──────────────────────────────────────────┘

🟢 Junior Level Path

  1. Contract Foundations: 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()
  2. Table & Indexing: How HashMap Works Internally, What is a Bucket in HashMap, How HashMap Determines Which Bucket to Put an Element In
  3. Collision Basics: What is a Collision in HashMap, How HashMap Handles Collisions
  4. Contract Pitfalls: What Happens If You Override equals() But Not hashCode(), What Happens If You Override hashCode() But Not equals()
  5. Null Support & Legacy: Can HashMap Store a null Key, Can HashMap Store a null Value, What is the Difference Between HashMap and Hashtable

🟡 Middle Level Path

  1. Treeification Thresholds: What Happens When 8 Elements Are Reached in One Bucket
  2. Key Mutability Hazards: 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
  3. Capacity, Load Factor & Resize: What is Load Factor in HashMap, What is Capacity in HashMap, When Does Rehashing Occur in HashMap, What Happens During Rehashing, How to Choose the Initial Capacity for HashMap
  4. Complexity & Multi-Threading: 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

🔴 Senior Level Path

  1. Bitwise Hash Spread Function: How HashMap Works Internally (Senior Section: XOR shift (h ^ (h >>> 16)))
  2. Rehashing Lo/Hi Bit Split: What Happens During Rehashing (Senior Section: Power-of-two bit partitioning without recomputing hash)
  3. Concurrent Map Architecture: What is ConcurrentHashMap and How Does It Differ from HashMap (CAS bucket initialization, synchronized bucket heads, CounterCell, ForwardingNode, Weakly Consistent Iterators)
  4. Weak References & Memory Leaks: What is WeakHashMap (WeakReference<K>, ReferenceQueue, expunging stale entries)
  5. Hash Flooding Attacks & Security: When Can Time Complexity Become O(n) (DoS hash collision attacks and Red-Black tree defense)
  6. Implementation Architecture Comparison: Which Map Implementation to Choose (Memory footprints, GC churn, caching patterns)

🎓 Quick Revision Invariants

🟢 Junior Essentials

  • Internal Array: Node<K,V>[] table backing array where index = (n - 1) & hash.
  • Contract Rule: If a.equals(b) is true, a.hashCode() == b.hashCode() MUST be true.
  • Collisions: Same hash code $\ne$ equal objects; collisions are chained in bucket linked lists.
  • Null Handling: HashMap allows exactly one null key (mapped to bucket 0) and multiple null values; Hashtable and ConcurrentHashMap forbid null.

🟡 Middle Essentials

  • Treeification (Java 8+): When bucket length reaches 8 AND table capacity $\ge$ 64, the bucket converts from a linked list into a Red-Black Tree (TreeNode, $O(\log N)$ lookup). If capacity $< 64$, the table resizes instead of treeifying.
  • Untreeify Threshold: When elements in a treeified bucket drop to 6 during resize or removal, it reverts to a linked list.
  • Load Factor (0.75): The optimal balance between memory utilization and collision frequency. Threshold = capacity * 0.75.
  • Mutable Key Anti-Pattern: Mutating a key after insertion changes its hash code, making it impossible to locate in its original bucket (memory leak + silent data loss).

🔴 Senior Essentials

  • Hash Spread: (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16) folds higher 16 bits into lower 16 bits to prevent lower-bit clustering.
  • Resize Lo/Hi Split: Because capacity doubles as a power of two, elements either stay at their current index i or move to i + oldCap based on a single bit check (e.hash & oldCap) == 0.
  • Java 7 vs Java 8 Multi-Threading: Java 7 Head Insertion during resize caused circular linked lists and 100% CPU infinite loops. Java 8 Tail Insertion eliminates circular loops, but concurrent writes without synchronization still cause silent lost updates and memory corruption.
  • ConcurrentHashMap Highload: Lock-free CAS for empty buckets, fine-grained bucket synchronization for populated heads, and sharded CounterCell[] array for high-throughput size() calculation.