Skip to content

Commit 9725bef

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

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Collection;
2+
import java.util.Comparator;
3+
import java.util.Set;
4+
import java.util.SortedMap;
5+
import java.util.TreeMap;
6+
7+
public class SortedMapDemo3 {
8+
public static void main(String[] args) {
9+
// SortedMap is an interface. TreeMap is a common implementation that
10+
// keeps keys in sorted order (natural order by default).
11+
SortedMap<String, Integer> map = new TreeMap<>();
12+
13+
// Put some entries
14+
map.put("Vivek", 91);
15+
map.put("Shubham", 99);
16+
map.put("Mohit", 78);
17+
map.put("Vipul", 77);
18+
19+
// Print full map (keys in sorted order)
20+
System.out.println("Full map (natural order): " + map);
21+
22+
// firstKey() and lastKey()
23+
System.out.println("First key: " + map.firstKey()); // smallest key
24+
System.out.println("Last key: " + map.lastKey()); // largest key
25+
26+
// headMap(toKey): returns a view of the map whose keys are strictly < toKey
27+
System.out.println("headMap(\"Vivek\"): " + map.headMap("Vivek"));
28+
29+
// tailMap(fromKey): returns a view of the map whose keys are >= fromKey
30+
System.out.println("tailMap(\"Vivek\"): " + map.tailMap("Vivek"));
31+
32+
// subMap(fromKey, toKey): keys in [fromKey, toKey)
33+
System.out.println("subMap(\"Mohit\", \"Vivek\"): " + map.subMap("Mohit", "Vivek"));
34+
35+
// keySet(), values(), entrySet() - examples of views
36+
Set<String> keys = map.keySet();
37+
Collection<Integer> values = map.values();
38+
System.out.println("Keys view: " + keys);
39+
System.out.println("Values view: " + values);
40+
41+
// containsKey / containsValue / get
42+
System.out.println("Contains key 'Mohit'? " + map.containsKey("Mohit"));
43+
System.out.println("Contains value 100? " + map.containsValue(100));
44+
System.out.println("Value for 'Vipul': " + map.get("Vipul"));
45+
46+
// Demonstrate custom comparator (descending) by creating a new TreeMap
47+
SortedMap<Integer, String> descMap = new TreeMap<>(Comparator.reverseOrder());
48+
descMap.put(91, "Vivek");
49+
descMap.put(99, "Shubham");
50+
descMap.put(78, "Mohit");
51+
descMap.put(77, "Vipul");
52+
System.out.println("Descending-key TreeMap: " + descMap);
53+
54+
// firstKey / lastKey reflect ordering of the map
55+
System.out.println("First key in descending map (highest): " + descMap.firstKey());
56+
System.out.println("Last key in descending map (lowest): " + descMap.lastKey());
57+
}
58+
}

0 commit comments

Comments
 (0)