Skip to content

Commit 8796101

Browse files
committed
Internal Working of SortedMap (Detailed Theory)
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 56bc33e commit 8796101

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Internal Working of SortedMap (Detailed Theory)
2+
3+
1. What is SortedMap?
4+
- `SortedMap` is an interface in Java that extends `Map`.
5+
- It ensures that **keys are stored in a sorted order**.
6+
- Sorting is either:
7+
- **Natural ordering** (keys must implement `Comparable` like Integer, String), OR
8+
- **Custom ordering** (using a `Comparator` provided at map creation).
9+
10+
Example Implementations:
11+
- `TreeMap` (most common implementation).
12+
- `ConcurrentSkipListMap` (thread-safe, concurrent alternative).
13+
14+
2. Storage Mechanism (TreeMap - default implementation):
15+
- Internally uses a **Red-Black Tree** (self-balancing binary search tree).
16+
- Every insertion/removal rebalances the tree → keeps operations efficient.
17+
- Guarantees **logarithmic time complexity (O(log n))** for `put`, `get`, and `remove`.
18+
19+
3. Ordering Logic:
20+
- Keys are compared using:
21+
- `compareTo()` (if using natural ordering), OR
22+
- `Comparator.compare()` (if a comparator is supplied).
23+
- Duplicate keys are NOT allowed.
24+
- Values can be duplicate since ordering is only applied on keys.
25+
26+
4. Important Internal Properties:
27+
- `firstKey()` → returns the smallest key.
28+
- `lastKey()` → returns the largest key.
29+
- `headMap(toKey)` → all keys less than `toKey`.
30+
- `tailMap(fromKey)` → all keys greater than or equal to `fromKey`.
31+
- `subMap(fromKey, toKey)` → range view (between two keys).
32+
33+
5. Null Handling:
34+
- `TreeMap` does NOT allow `null` keys (throws `NullPointerException`).
35+
- `null` values are allowed (but discouraged).
36+
- Reason: comparator/compareTo() cannot handle `null` key comparisons.
37+
38+
6. Performance:
39+
- Insertion → O(log n)
40+
- Lookup (get) → O(log n)
41+
- Deletion → O(log n)
42+
- Iteration → O(n), but in **sorted order** of keys.
43+
44+
7. Iteration Order:
45+
- Unlike HashMap (unordered) or LinkedHashMap (insertion/access order),
46+
SortedMap always iterates in **ascending order of keys**.
47+
- If a custom comparator is given, iteration follows that comparator’s order.
48+
49+
──────────────────────────────────────────────────────────────
50+
📝 ASCII Representation (Red-Black Tree Example)
51+
──────────────────────────────────────────────────────────────
52+
53+
Insert keys: 20, 10, 30, 25
54+
55+
(20) Black
56+
/ \
57+
(10)R (30)B
58+
/
59+
(25)R
60+
61+
- Keys are arranged in sorted order.
62+
- Red-Black Tree ensures balance for O(log n) operations.
63+
- In-order traversal → 10 → 20 → 25 → 30.
64+
65+
──────────────────────────────────────────────────────────────
66+
8. Practical Applications:
67+
- Range queries: e.g., "give me all students with roll numbers between 100 and 200".
68+
- Navigation operations (finding closest keys).
69+
- Useful in scheduling, ranking systems, or interval problems.
70+
71+
──────────────────────────────────────────────────────────────
72+
✔ Key Takeaways:
73+
- `SortedMap` maintains keys in sorted order (natural or custom).
74+
- Internally backed by balanced trees (commonly Red-Black Tree).
75+
- Operations: O(log n).
76+
- No null keys allowed.
77+
- Best choice when **ordering of keys is essential**.

0 commit comments

Comments
 (0)