Skip to content

Commit f0475df

Browse files
committed
HashMap vs IdentityHashMap — Quick Comparison
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ae6e2b7 commit f0475df

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
HashMap vs IdentityHashMap — Quick Comparison
2+
3+
Feature | HashMap | IdentityHashMap
4+
--------------------------|-------------------------------------------|-----------------------------------------------------------
5+
Key Equality Check | Uses `.equals()` for value equality | Uses `==` (reference equality)
6+
Hash Code Used | `key.hashCode()` (can be overridden) | `System.identityHashCode(key)` (reference-based)
7+
Same Content Keys | Treated as SAME key (overwrites value) | Treated as DIFFERENT keys (both stored separately)
8+
Example | Two Strings "Akshit" → 1 entry | Two Strings "Akshit" → 2 separate entries
9+
Null Keys / Values | Allows one null key, multiple null values | Allows null keys and values
10+
Performance | O(1) average for put/get | O(1) average for put/get (slightly higher memory)
11+
Iteration Order | Not guaranteed | Not guaranteed
12+
Use Case | General-purpose maps (value-based keys) | Specialized use cases requiring identity tracking
13+
Typical Application | Caches, lookups, configs, DB mappings | Object pools, serialization internals, reference tracking

0 commit comments

Comments
 (0)