Skip to content

Commit 494ccda

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.IdentityHashMap;
2+
import java.util.Map;
3+
4+
public class IdentityHashMapDemo {
5+
public static void main(String[] args) {
6+
Map<String, Integer> map = new IdentityHashMap<>();
7+
8+
/*
9+
* IdentityHashMap ek special Map implementation hai jo keys ko compare karta hai
10+
* sirf reference equality (==) ke basis par.
11+
12+
* Matlab:
13+
* - Normal HashMap me comparison hota hai equals() aur hashCode() ke basis par.
14+
* - IdentityHashMap me comparison hota hai (k1 == k2).
15+
16+
* Example:
17+
* Agar do alag String objects "Akshit" banaye (new String("Akshit")),
18+
* to HashMap unko equal maan lega (kyunki equals() true hoga).
19+
* Lekin IdentityHashMap unko alag treat karega (kyunki == false hoga).
20+
*/
21+
22+
// Agar chaho to neeche ka HashMap wala line use karke difference check kar sakte ho:
23+
// Map<String, Integer> map = new HashMap<>();
24+
25+
String key1 = new String("Akshit");
26+
String key2 = new String("Akshit");
27+
28+
map.put(key1, 90);
29+
map.put(key2, 92);
30+
31+
// identityHashCode() ek utility hai jo object ka "memory reference based hash" deta hai.
32+
// Isse pata chalta hai ki dono objects alag-alag memory me store hain.
33+
System.out.println(System.identityHashCode(key1));
34+
System.out.println(System.identityHashCode(key2));
35+
36+
/*
37+
* Normal HashMap me:
38+
* - key1.equals(key2) true hota (kyunki String content same hai).
39+
* - hashCode bhi same aata → same bucket me jaata.
40+
* - Result: sirf ek entry banti, value overwrite ho jaati.
41+
*
42+
* IdentityHashMap me:
43+
* - key1 == key2 false hai (alag objects hai).
44+
* - Isliye dono alag keys treat hote hain.
45+
* - Result: 2 alag entries banti hain.
46+
*/
47+
48+
// System.out.println(key1.equals(key2)); // Ye true hoga (normal equals check)
49+
System.out.println(map);
50+
}
51+
}

0 commit comments

Comments
 (0)