Skip to content

Commit 6d3cd41

Browse files
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

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+
import java.util.SortedMap;
2+
import java.util.TreeMap;
3+
4+
public class SortedMapDemo1 {
5+
public static void main(String[] args) {
6+
SortedMap<Integer, String> map = new TreeMap<>();
7+
// In SortedMap order is maintained low to high (natural order for Integer keys).
8+
9+
// If you want descending order, you could construct TreeMap with a comparator:
10+
// SortedMap<Integer, String> map = new TreeMap<>((a,b)->b-a);
11+
12+
map.put(91, "Vivek");
13+
map.put(99, "Shubham");
14+
map.put(78, "Mohit");
15+
map.put(77, "Vipul");
16+
17+
// Print the whole map (entries are shown in ascending key order)
18+
System.out.println(map);
19+
20+
// First (smallest) key in the map
21+
System.out.println(map.firstKey());
22+
23+
// Last (largest) key in the map
24+
System.out.println(map.lastKey());
25+
26+
// headMap(91) -> returns a view of the portion whose keys are strictly less than 91
27+
System.out.println(map.headMap(91));
28+
29+
// tailMap(91) -> returns a view of the portion whose keys are greater than or equal to 91
30+
System.out.println(map.tailMap(91));
31+
32+
// Example note: subMap() can be used for a range view (not shown here)
33+
// map.subMap(fromKey, toKey)
34+
35+
// Sorting is based on keys (not values)
36+
System.out.println(map);
37+
38+
// Get value by key
39+
System.out.println(map.get(77));
40+
41+
// Check existence of a key
42+
System.out.println(map.containsKey(78));
43+
44+
// Check existence of a value (note: containsValue checks values, not keys)
45+
System.out.println(map.containsValue(77));
46+
}
47+
}
48+
49+
/*
50+
• TreeMap implements SortedMap and keeps entries sorted by keys.
51+
• Using a TreeMap<Integer, String> will order entries by Integer natural order (ascending).
52+
• firstKey() / lastKey() return the smallest / largest key respectively (according to the map’s ordering).
53+
• headMap(k) returns a view with keys < k.
54+
• tailMap(k) returns a view with keys >= k.
55+
• get(key), containsKey(key), and containsValue(value) are standard lookup operations
56+
(note containsValue searches values, so containsValue(77) is false here because 77 is a key not a value).
57+
58+
Why ordering matters
59+
• TreeMap uses a balanced tree (Red-Black tree) internally.
60+
• Most operations are O(log n): put, get, remove, navigation methods.
61+
• If you need fast unordered lookups and don’t care about order, HashMap is typically faster (O(1) avg).
62+
• Use TreeMap when you need sorted traversal or range queries (subMap/headMap/tailMap).
63+
64+
Important gotchas / reminders
65+
• TreeMap does NOT accept null keys (will throw NullPointerException). It does accept null values.
66+
• If you supply a custom comparator (e.g., descending),
67+
all ordering and navigation semantics change to follow that comparator.
68+
• headMap and tailMap return views — modifying them modifies the original map.
69+
70+
How keys are ordered:
71+
Inserted keys: 91, 99, 78, 77
72+
73+
Tree in-order traversal (ascending):
74+
77 78 91 99
75+
76+
Printed map: {77=Vipul, 78=Mohit, 91=Vivek, 99=Shubham}
77+
*/

0 commit comments

Comments
 (0)