Commit b9aa0cd
committed
feat(SortedMapDemo2): demonstrate TreeMap with custom descending key order
What
- Added SortedMapDemo2 class.
- Created TreeMap with custom Comparator `(a,b) -> b-a` (descending order by keys).
- Inserted keys {91, 99, 78, 77} with values.
- Printed map showing descending order of keys.
- Demonstrated navigation methods in reversed order:
• firstKey() → highest key
• lastKey() → lowest key
• headMap(), tailMap() behavior under descending comparator
- Checked key and value existence with containsKey/containsValue.
Why
- To illustrate how a custom Comparator alters TreeMap’s natural ordering.
- Show differences in semantics of navigation methods when order is reversed.
- Reinforce concept that all range queries follow the comparator, not natural ascending.
How
- Constructed TreeMap with custom comparator.
- Added entries out of order.
- Map sorted internally in descending order: {99=Shubham, 91=Vivek, 78=Mohit, 77=Vipul}.
- Demonstrated:
• firstKey() = 99 (largest)
• lastKey() = 77 (smallest)
• headMap(91) = keys greater than 91 (→ {99=Shubham})
• tailMap(91) = keys <= 91 (→ {91=Vivek, 78=Mohit, 77=Vipul})
- Key/value checks:
• containsKey(78) → true
• containsValue(77) → false (77 is key, not value).
Logic
- Data structure: TreeMap (Red-Black Tree).
- Comparator defines ordering → descending order of integer keys.
- Complexity:
- put/get/remove = O(log n).
- headMap/tailMap adapt to custom comparator rules.
- Important:
- With descending comparator, “first” = greatest element, “last” = smallest.
- headMap/tailMap follow comparator semantics, not numeric natural meaning.
Real-life applications
- Useful for scenarios like leaderboards, where highest score should appear first.
- Descending order maps for stock prices, high-priority tasks, or ranking systems.
- Great alternative to manual reverse traversals of ascending TreeMaps.
Summary
- Custom Comparator allows TreeMap to order keys in descending order.
- Navigation methods (firstKey, lastKey, headMap, tailMap) adapt to this custom order.
- TreeMap remains O(log n) efficient while supporting ordered views in both directions.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6d3cd41 commit b9aa0cd
File tree
1 file changed
+38
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Sorted Map/src
1 file changed
+38
-0
lines changedLines changed: 38 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 | + | |
0 commit comments