Skip to content

Commit a147014

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

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.*;
2+
3+
public class SortedMapDemo4 {
4+
public static void main(String[] args) {
5+
// SortedMap is an interface, TreeMap is its implementation.
6+
// TreeMap maintains keys in sorted (natural) order by default.
7+
SortedMap<String, Integer> sortedMap = new TreeMap<>();
8+
9+
// Adding entries
10+
sortedMap.put("Vivek", 91);
11+
sortedMap.put("Shubham", 99);
12+
sortedMap.put("Mohit", 78);
13+
sortedMap.put("Vipul", 77);
14+
15+
// Printing full map - automatically sorted by keys (ascending order)
16+
System.out.println("Full Map: " + sortedMap);
17+
18+
// firstKey() - smallest key
19+
System.out.println("First Key: " + sortedMap.firstKey());
20+
21+
// lastKey() - largest key
22+
System.out.println("Last Key: " + sortedMap.lastKey());
23+
24+
// headMap(toKey) - keys < toKey
25+
System.out.println("headMap(\"Vivek\"): " + sortedMap.headMap("Vivek"));
26+
27+
// tailMap(fromKey) - keys >= fromKey
28+
System.out.println("tailMap(\"Vivek\"): " + sortedMap.tailMap("Vivek"));
29+
30+
// subMap(fromKey, toKey) - keys in [fromKey, toKey)
31+
System.out.println("subMap(\"Mohit\", \"Vivek\"): " + sortedMap.subMap("Mohit", "Vivek"));
32+
33+
// containsKey() and containsValue()
34+
System.out.println("Contains key 'Shubham'? " + sortedMap.containsKey("Shubham"));
35+
System.out.println("Contains value 100? " + sortedMap.containsValue(100));
36+
37+
// Get a value by key
38+
System.out.println("Value for 'Vipul': " + sortedMap.get("Vipul"));
39+
40+
// Views
41+
System.out.println("Key Set: " + sortedMap.keySet());
42+
System.out.println("Values: " + sortedMap.values());
43+
44+
45+
// Example with custom Comparator
46+
SortedMap<Integer, String> descMap = new TreeMap<>(Comparator.reverseOrder());
47+
descMap.put(91, "Vivek");
48+
descMap.put(99, "Shubham");
49+
descMap.put(78, "Mohit");
50+
descMap.put(77, "Vipul");
51+
52+
System.out.println("\nCustom Comparator (Descending Order): " + descMap);
53+
System.out.println("First Key (Highest): " + descMap.firstKey());
54+
System.out.println("Last Key (Lowest): " + descMap.lastKey());
55+
}
56+
}

0 commit comments

Comments
 (0)