Skip to content

Commit 1acc585

Browse files
committed
feat(SortedMapDemo): demonstrate TreeMap with custom reverse-order comparator
What - Added `SortedMapDemo` class using a `TreeMap` with a custom comparator for descending key order. - Showcased core `SortedMap` operations: - `put()` to insert entries. - `get()`, `containsKey()`, and `containsValue()`. - Views: `firstKey()`, `lastKey()`, `headMap()`, and `tailMap()` (commented for demo flexibility). Why - To illustrate how `SortedMap` works in Java and how `TreeMap` maintains key ordering. - Useful for scenarios requiring ordered key-value mappings with custom sorting logic. How - Created a `TreeMap<Integer, String>` with comparator `(a, b) -> b - a` to enforce descending order of keys. - Inserted sample student scores (`99`, `91`, `78`, `77`) mapped to names. - Highlighted the effect of the comparator on order and how to query ranges. Logic - Keys are sorted in reverse order due to comparator. - Insert sequence: 91, 99, 78, 77. - Resulting order: 99 → 91 → 78 → 77. - `firstKey()` → 99 (highest). - `lastKey()` → 77 (lowest). - `headMap(91)` → keys greater than 91 in descending order → [99]. - `tailMap(91)` → keys ≤ 91 in descending order → [91, 78, 77]. Real-life applications - Maintaining leaderboards (highest score first). - Stock price tickers (most recent/highest values prioritized). - Priority-based mappings where descending order of keys is important. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8796101 commit 1acc585

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.SortedMap;
2+
import java.util.TreeMap;
3+
4+
/**
5+
* Demonstrates SortedMap using TreeMap with a custom comparator (reverse order).
6+
* This file focuses on basic SortedMap operations: put, get, containsKey, containsValue, and views.
7+
8+
* Note: TreeMap implements SortedMap and keeps keys in sorted order determined by either:
9+
* - natural ordering (Comparable), or
10+
* - a provided Comparator (as shown here).
11+
*/
12+
13+
public class SortedMapDemo {
14+
public static void main(String[] args) {
15+
// Create a SortedMap backed by a TreeMap with a custom comparator (descending order).
16+
SortedMap<Integer, String> map = new TreeMap<>((a, b) -> b - a);
17+
18+
// Insert entries (keys will be kept in descending order because of comparator).
19+
map.put(91, "Vivek");
20+
map.put(99, "Shubham");
21+
map.put(78, "Mohit");
22+
map.put(77, "Vipul");
23+
24+
// Access/queries (these do not change map ordering)
25+
map.get(77); // returns "Vipul"
26+
map.containsKey(78); // true
27+
map.containsValue(77); // false (77 is a key, not a value)
28+
29+
// Uncomment the lines below to inspect map contents and views:
30+
// System.out.println(map); // Shows entries in descending key order
31+
// System.out.println(map.firstKey()); // Highest key (because we used descending comparator)
32+
// System.out.println(map.lastKey()); // Lowest key
33+
// System.out.println(map.headMap(91)); // Keys strictly greater (in comparator order) than 91
34+
// System.out.println(map.tailMap(91)); // Keys <= 91 in this comparator ordering
35+
}
36+
}
37+
38+
/*
39+
Insert: 91, 99, 78, 77
40+
41+
Comparator: descending
42+
Print order: 99, 91, 78, 77
43+
map.firstKey() -> 99
44+
map.lastKey() -> 77
45+
*/

0 commit comments

Comments
 (0)