Commit 6d3cd41
committed
feat(SortedMapDemo1): add demo of TreeMap with SortedMap navigation methods
What
- Added SortedMapDemo1 class.
- Demonstrated TreeMap (implements SortedMap) with Integer keys and String values.
- Inserted keys {91, 99, 78, 77} with values.
- Printed map and used navigation/look-up methods:
• firstKey(), lastKey()
• headMap(), tailMap()
• get(), containsKey(), containsValue()
- Commented on custom comparators for descending order.
Why
- To illustrate how TreeMap maintains natural ascending order of keys.
- Show how SortedMap interface methods provide range views and navigation.
- Compare differences between key-based operations and value checks.
How
- Constructed a TreeMap<Integer,String>.
- Inserted entries out of order.
- Printed TreeMap → automatically sorted as {77=Vipul, 78=Mohit, 91=Vivek, 99=Shubham}.
- Called navigation methods:
• firstKey() = 77
• lastKey() = 99
• headMap(91) = {77=Vipul, 78=Mohit}
• tailMap(91) = {91=Vivek, 99=Shubham}
- Checked key/value presence:
• containsKey(78) → true
• containsValue(77) → false (since 77 is a key not a value).
Logic
- Data structure: TreeMap (Red-Black Tree).
- Ordering:
- Keys sorted by natural order unless custom Comparator supplied.
- Example insertion order {91, 99, 78, 77} sorted as {77, 78, 91, 99}.
- Complexity:
- put/get/remove = O(log n).
- Navigation methods also O(log n).
- Notes:
- headMap(), tailMap() produce live views backed by original map.
- Null keys not allowed (NullPointerException).
- Null values allowed.
Real-life applications
- Range queries, e.g., finding users with IDs in a range.
- Navigation in leaderboards, stock prices, or priority-based tasks.
- Provides predictable sorted traversal compared to HashMap.
- Used when both key-based lookup and sorted order are required.
Summary
- TreeMap auto-sorts keys using Red-Black tree.
- SortedMap interface provides powerful navigation methods.
- Great for use cases needing ordered or ranged access.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 1acc585 commit 6d3cd41
File tree
1 file changed
+77
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Sorted Map/src
1 file changed
+77
-0
lines changedLines changed: 77 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 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
0 commit comments