Skip to content

Commit b9aa0cd

Browse files
committed
feat(SortedMapDemo2): demonstrate TreeMap with custom descending key order
What - Added SortedMapDemo2 class. - Created TreeMap with custom Comparator `(a,b) -> b-a` (descending order by keys). - Inserted keys {91, 99, 78, 77} with values. - Printed map showing descending order of keys. - Demonstrated navigation methods in reversed order: • firstKey() → highest key • lastKey() → lowest key • headMap(), tailMap() behavior under descending comparator - Checked key and value existence with containsKey/containsValue. Why - To illustrate how a custom Comparator alters TreeMap’s natural ordering. - Show differences in semantics of navigation methods when order is reversed. - Reinforce concept that all range queries follow the comparator, not natural ascending. How - Constructed TreeMap with custom comparator. - Added entries out of order. - Map sorted internally in descending order: {99=Shubham, 91=Vivek, 78=Mohit, 77=Vipul}. - Demonstrated: • firstKey() = 99 (largest) • lastKey() = 77 (smallest) • headMap(91) = keys greater than 91 (→ {99=Shubham}) • tailMap(91) = keys <= 91 (→ {91=Vivek, 78=Mohit, 77=Vipul}) - Key/value checks: • containsKey(78) → true • containsValue(77) → false (77 is key, not value). Logic - Data structure: TreeMap (Red-Black Tree). - Comparator defines ordering → descending order of integer keys. - Complexity: - put/get/remove = O(log n). - headMap/tailMap adapt to custom comparator rules. - Important: - With descending comparator, “first” = greatest element, “last” = smallest. - headMap/tailMap follow comparator semantics, not numeric natural meaning. Real-life applications - Useful for scenarios like leaderboards, where highest score should appear first. - Descending order maps for stock prices, high-priority tasks, or ranking systems. - Great alternative to manual reverse traversals of ascending TreeMaps. Summary - Custom Comparator allows TreeMap to order keys in descending order. - Navigation methods (firstKey, lastKey, headMap, tailMap) adapt to this custom order. - TreeMap remains O(log n) efficient while supporting ordered views in both directions. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 6d3cd41 commit b9aa0cd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.SortedMap;
2+
import java.util.TreeMap;
3+
4+
public class SortedMapDemo2 {
5+
public static void main(String[] args) {
6+
// TreeMap with custom Comparator (descending order by keys)
7+
SortedMap<Integer, String> map = new TreeMap<>((a, b) -> b - a);
8+
9+
map.put(91, "Vivek");
10+
map.put(99, "Shubham");
11+
map.put(78, "Mohit");
12+
map.put(77, "Vipul");
13+
14+
// Print map -> keys will appear in descending order now
15+
System.out.println(map);
16+
17+
// First (now largest, since descending order)
18+
System.out.println("First Key (highest): " + map.firstKey());
19+
20+
// Last (now smallest, since descending order)
21+
System.out.println("Last Key (lowest): " + map.lastKey());
22+
23+
// headMap(91) -> in descending, gives keys greater than 91
24+
System.out.println("headMap(91): " + map.headMap(91));
25+
26+
// tailMap(91) -> in descending, gives keys <= 91
27+
System.out.println("tailMap(91): " + map.tailMap(91));
28+
29+
// Get by key
30+
System.out.println("Value for key 77: " + map.get(77));
31+
32+
// Key existence
33+
System.out.println("Contains key 78? " + map.containsKey(78));
34+
35+
// Value existence
36+
System.out.println("Contains value 77? " + map.containsValue(77));
37+
}
38+
}

0 commit comments

Comments
 (0)