Skip to content

Commit 4a18de4

Browse files
committed
LinkedList Role In Internal Working Of HashMap:
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7c9eba0 commit 4a18de4

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
LinkedList Role In Internal Working Of HashMap:
2+
3+
1. HashMap Storage Basic:
4+
- HashMap ek **array of buckets** use karta hai.
5+
- Har bucket ek **LinkedList** (ya Java 8 se aage kabhi kabhi Tree) hold kar sakta hai.
6+
- Jab collision hoti hai (multiple keys same bucket index pe map ho jaati hain), tab LinkedList use hoti hai.
7+
8+
2. Insertion Process:
9+
- Har key ka hashCode nikala jata hai.
10+
- HashCode ko bucket index me convert kiya jata hai → `index = hashCode % capacity`.
11+
- Agar bucket khali hai → direct entry add hoti hai.
12+
- Agar bucket me already entry hai (collision) → LinkedList ke end me new node attach hoti hai.
13+
14+
Example:
15+
- Suppose HashMap capacity = 10
16+
- Key `"ABC"` aur `"CBA"` dono ka hashCode % 10 = 8
17+
- Dono entries bucket[8] me add hongi, aur LinkedList banegi.
18+
19+
3. LinkedList Node:
20+
- Har node ek **Map.Entry object** hota hai:
21+
- key
22+
- value
23+
- next (LinkedList ka next node pointer)
24+
25+
Structure:
26+
27+
bucket[8] → [key1=value1] → [key2=value2] → [key3=value3]
28+
29+
4. Searching (get()):
30+
- Key ka hashCode se bucket index nikalo.
31+
- Us bucket ke LinkedList me traverse karo.
32+
- `.equals()` method se key match karna padta hai.
33+
- Match milte hi value return ho jati hai.
34+
35+
❌ Agar LinkedList badi ho jaye toh search O(n) ho sakta hai.
36+
37+
5. Java 8 Improvement:
38+
- Agar ek bucket me **bahut zyada collisions** ho jayein (default threshold ~8 nodes),
39+
toh LinkedList ko **Balanced Tree (Red-Black Tree)** me convert kar dete hain.
40+
- Isse search time O(n) → O(log n) ho jata hai.
41+
42+
6. Removal:
43+
- Key ka bucket index find karo.
44+
- LinkedList traverse karke matching node dhoondo.
45+
- Pichle node ka `next` pointer ko update karke node delete kar do.
46+
47+
7. Complexity:
48+
- Average case (good hash function): O(1)
49+
- Worst case (sab same bucket me chale gaye): O(n) → LinkedList traversal
50+
- Java 8 ke baad worst case improved to O(log n) with Tree.
51+
52+
---
53+
54+
ASCII Diagram Example
55+
---------------------
56+
57+
Initial (empty HashMap, capacity = 10):
58+
59+
bucket[0] null
60+
bucket[1] null
61+
62+
bucket[8] null
63+
bucket[9] null
64+
65+
Insert "ABC"=10 at index 8:
66+
67+
bucket[8] → [ABC=10]
68+
69+
Insert "CBA"=20 (same index 8):
70+
71+
bucket[8] → [ABC=10] → [CBA=20]
72+
73+
Insert "BAC"=30 (same index 8):
74+
75+
bucket[8] → [ABC=10] → [CBA=20] → [BAC=30]
76+
77+
Now bucket[8] is a LinkedList of 3 nodes.
78+
79+
---
80+
81+
Quick Recap
82+
-----------
83+
✔ LinkedList is used in HashMap to handle collisions.
84+
✔ Each bucket can hold multiple entries as a chain (LinkedList).
85+
✔ Searching means traversing LinkedList inside that bucket.
86+
✔ Java 8 improved it with Tree when collisions become heavy.
87+
88+

0 commit comments

Comments
 (0)