Commit a147014
committed
feat(SortedMapDemo4): add demo of TreeMap with natural and custom comparator ordering
What
- Added SortedMapDemo4 class.
- Demonstrated TreeMap with:
• Natural ordering of String keys (alphabetical ascending).
• Navigation methods: firstKey, lastKey, headMap, tailMap, subMap.
• Lookup methods: containsKey, containsValue, get.
• Views: keySet() and values().
- Added second TreeMap with Comparator.reverseOrder() to show descending Integer keys.
Why
- To illustrate how SortedMap (via TreeMap) behaves with default natural ordering and with a custom comparator.
- Show the difference in semantics when ordering is reversed.
- Teach usage of range queries (headMap, tailMap, subMap) and map views.
How
- Constructed TreeMap<String,Integer> with default natural ordering.
- Inserted entries:
• Vivek=91, Shubham=99, Mohit=78, Vipul=77.
- Verified ascending key order in output.
- Called navigation methods:
• firstKey() = Mohit, lastKey() = Vivek.
• headMap("Vivek") = {Mohit=78, Shubham=99, Vipul=77}.
• tailMap("Vivek") = {Vivek=91}.
• subMap("Mohit","Vivek") = {Mohit=78, Shubham=99, Vipul=77}.
- Looked up key/value presence and retrieved a value.
- Printed keySet() and values() views.
- Constructed TreeMap<Integer,String> with Comparator.reverseOrder().
• Inserted entries {91=Vivek, 99=Shubham, 78=Mohit, 77=Vipul}.
• Printed descending order {99=Shubham, 91=Vivek, 78=Mohit, 77=Vipul}.
• Verified firstKey() = 99, lastKey() = 77.
Logic
- Data structure: TreeMap (Red-Black tree).
- Complexity: put/get/remove = O(log n).
- Range methods (headMap, tailMap, subMap) produce live backed views.
- Ordering depends on comparator:
• Natural order: lexicographic ascending for Strings.
• Comparator.reverseOrder(): numeric descending for Integers.
- firstKey()/lastKey() reflect current comparator order.
- Null keys not allowed, null values allowed.
Real-life applications
- Maintaining a sorted directory of users (by name).
- Leaderboards sorted by scores (descending comparator).
- Range queries (e.g., find users with IDs in certain ranges).
- Easy retrieval of smallest/largest keys in current ordering.
Summary
- TreeMap provides sorted storage and navigation for keys.
- Natural order vs custom comparator order changes all key-based operations.
- Range views (headMap, tailMap, subMap) are efficient and backed by the tree.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9725bef commit a147014
File tree
1 file changed
+56
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Sorted Map/src
1 file changed
+56
-0
lines changedLines changed: 56 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 | + | |
0 commit comments