Commit 35e353e
committed
feat(HashCodeAndEqualsMethod): demonstrate importance of overriding equals() and hashCode() in HashMap keys
What
- Added `HashCodeAndEqualsMethod` class with a `main` method.
- Introduced a custom `Person` class that overrides `equals()` and `hashCode()`.
- Demonstrates how `HashMap` uses these methods for key comparison and replacement.
- Also included an example with `Map<String, Integer>` to show default behavior.
Why
- To explain how Java collections like `HashMap` rely on both `hashCode()` and `equals()` to store and retrieve values.
- Shows that without proper overrides, logically equal objects may still be treated as different keys.
- Reinforces the contract: if two objects are equal (`equals()` returns true), their `hashCode()` must be the same.
How
1. Created three `Person` objects:
- `p1("Alice", 1)`
- `p2("Bob", 2)`
- `p3("Alice", 1)` (same values as p1).
2. Inserted all three into a `HashMap<Person, String>`.
- `p1 → "Engineer"`
- `p2 → "Designer"`
- `p3 → "Manager"`
3. Because `p1.equals(p3)` is true and both have same hashCode, `p3` replaces the value of `p1` inside the map.
4. Verified by printing: map size is `2` and `p1`/`p3` return `"Manager"`.
5. Added a `Map<String, Integer>` demo:
- `"Shubham"` added twice → second value replaces first.
- `"Neha"` added once.
Logic
- Step 1: Insert keys into map.
- Step 2: HashMap computes index using `hashCode()`.
- Step 3: If multiple keys fall in same bucket, it checks `equals()`.
- Step 4: If equal → replaces value; if not → inserts new entry.
- Step 5: Demonstrate with custom objects (`Person`) and built-in type (`String`).
Real-life Applications
- Correct behavior in caching, sets, and maps where uniqueness of keys is required.
- Critical when using objects as keys in `HashMap`, `HashSet`, `Hashtable`.
- Avoids subtle bugs where duplicates may incorrectly appear due to missing overrides.
Notes
- Always override `equals()` and `hashCode()` together.
- Use `Objects.hash()` for concise hashCode implementations.
- Use `Objects.equals()` for safe null-aware comparisons in `equals()`.
- Inconsistent implementation can break contract: two equal objects must always have the same hash code.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 67216bb commit 35e353e
File tree
2 files changed
+126
-13
lines changed- Section 25 Collections Frameworks/Map Interface/HashMap/src/HashCodeAndEqualsMethod
2 files changed
+126
-13
lines changedSection 25 Collections Frameworks/Map Interface/HashMap/src/HashCodeAndEqualsMethod/Code Recap.txt
Lines changed: 121 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | 34 | | |
43 | 35 | | |
44 | 36 | | |
| |||
80 | 72 | | |
81 | 73 | | |
82 | 74 | | |
83 | | - | |
| 75 | + | |
0 commit comments