Commit 494ccda
committed
feat(IdentityHashMapDemo): add example showing reference-based key comparison
What
- Added IdentityHashMapDemo class.
- Created an IdentityHashMap<String,Integer>.
- Inserted two distinct String objects with the same content `"Akshit"`.
- Printed System.identityHashCode() for both keys.
- Printed final map contents.
Why
- To demonstrate the difference between HashMap and IdentityHashMap:
• HashMap compares keys using equals() + hashCode().
• IdentityHashMap compares keys using reference equality (==).
- Showcases how two logically equal Strings can be treated as different keys.
How
- Constructed key1 and key2 using `new String("Akshit")` to ensure different references.
- Inserted (key1, 90) and (key2, 92) into the IdentityHashMap.
- Because key1 != key2 (reference inequality), both entries are stored.
- Printing identityHashCode confirms distinct memory references.
Logic
- Inputs:
- key1 = new String("Akshit")
- key2 = new String("Akshit")
- Processing:
- IdentityHashMap.put(key1,90) → adds entry.
- IdentityHashMap.put(key2,92) → treated as a separate entry.
- Outputs:
- Two identityHashCodes (different values).
- Map contents with two entries.
- Constraints:
- IdentityHashMap does not allow null keys comparison by content; only by reference.
- Complexity:
- O(1) average for put/get.
- Behavior difference only in equality check.
Real-life applications
- Useful in scenarios where object identity matters more than logical equality:
• Caching frameworks where exact object reference is required.
• Serialization/deserialization cycles.
• Handling proxy objects where equals() may be overridden.
- Rarely used in business logic; mostly in JVM internals or advanced libraries.
Notes
- Normal HashMap would have only one entry (second value overwrites the first).
- IdentityHashMap preserves both entries because reference equality differs.
- Demonstrates the subtle but important difference in equality semantics.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent 7f818e5 commit 494ccda
1 file changed
Lines changed: 51 additions & 0 deletions
File tree
- Section 25 Collections Frameworks/Map Interface/Identity HashMap/src
Lines changed: 51 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 | + | |
0 commit comments