Commit 9725bef
committed
feat(SortedMapDemo3): showcase TreeMap operations with natural and custom ordering
What
- Added SortedMapDemo3 class.
- Demonstrates TreeMap with:
• Natural ordering of String keys (alphabetical).
• Standard navigation methods: firstKey, lastKey, headMap, tailMap, subMap.
• Views: keySet(), values(), entrySet().
• Lookup operations: containsKey, containsValue, get.
- Added second TreeMap with Comparator.reverseOrder() for descending integer keys.
- Printed behavior of navigation methods under descending order.
Why
- To illustrate the versatility of TreeMap / SortedMap API:
• How it maintains sorted keys automatically.
• How range queries (headMap, tailMap, subMap) work.
• How ordering semantics change when using custom Comparator.
- Reinforces differences between key views, value views, and entry sets.
How
- Constructed TreeMap<String,Integer> with default natural order:
• Inserts: Vivek=91, Shubham=99, Mohit=78, Vipul=77.
• Tree sorted by key alphabetically → {Mohit=78, Shubham=99, Vipul=77, Vivek=91}.
- Demonstrated:
• firstKey() = Mohit, lastKey() = Vivek.
• headMap("Vivek") → keys < "Vivek".
• tailMap("Vivek") → keys ≥ "Vivek".
• subMap("Mohit","Vivek") → bounded range [Mohit,Vivek).
• keySet() = view of keys, values() = view of values.
• containsKey("Mohit") → true, containsValue(100) → false, get("Vipul") → 77.
- Constructed TreeMap<Integer,String> with reverse order comparator:
• Inserts keys {91,99,78,77}.
• Sorted descending → {99=Shubham,91=Vivek,78=Mohit,77=Vipul}.
• firstKey() = 99, lastKey() = 77 under descending rules.
Logic
- Data structure: TreeMap (Red-Black tree).
- Complexity:
- put/get/remove = O(log n).
- headMap/tailMap/subMap produce backed views (changes affect original map).
- Ordering semantics:
- Natural order: Strings sorted lexicographically.
- Custom comparator: Integers sorted descending.
- firstKey()/lastKey() adapt to comparator’s order, not numeric magnitude.
- Keys must be mutually comparable; null keys not allowed in TreeMap.
Real-life applications
- Directory-style lookups sorted by names.
- Range queries (subMap for bounded searches).
- Reverse leaderboards / ranking systems with descending comparators.
- Efficient retrieval of smallest/largest keys in O(log n).
Summary
- TreeMap automatically sorts keys by natural or custom order.
- SortedMap API offers powerful range and view operations.
- Custom comparators change semantics of navigation methods consistently.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b9aa0cd commit 9725bef
File tree
1 file changed
+58
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Sorted Map/src
1 file changed
+58
-0
lines changedLines changed: 58 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 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
0 commit comments