-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathTreeMap_Implementation.java
64 lines (44 loc) · 2.02 KB
/
TreeMap_Implementation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
class TreeMap_Implementation {
//A TreeMap is a map implementation where they are always sorted based on the natural ordering of the keys,
// or based on a custom Comparator that you can provide at the time of creation of the TreeMap.
//
public static void main(String[] args) {
// Creating a TreeMap
SortedMap<String, String> names = new TreeMap<>();
// Adding new key-value pairs to a TreeMap
names.put("Aanchal", "girl");
names.put("Rahul", "boy");
names.put("Arjun", "boy");
names.put("Karen", "girl");
names.put("Salman", "boy");
// Printing the TreeMap (Output will be sorted based on keys)
System.out.println(names);
System.out.println(((TreeMap<String, String>) names).firstEntry());
System.out.print(((TreeMap<String, String>) names).lastEntry());
SortedMap<String, String> names_2 = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
});
// Adding new key-value pairs to a TreeMap
names_2.put("Aanchal", "girl");
names_2.put("Rahul", "boy");
names_2.put("Arjun", "boy");
names_2.put("Karen", "girl");
names_2.put("Salman", "boy");
// Printing the TreeMap (The keys will be sorted based on the supplied comparator in descending order.)
System.out.println(names_2);
System.out.println(((TreeMap<String, String>) names_2).firstEntry());
System.out.print(((TreeMap<String, String>) names_2).lastEntry());
}
}
//Output
//{Aanchal=girl, Arjun=boy, Karen=girl, Rahul=boy, Salman=boy}
//Aanchal=girl
//Salman=boy{Salman=boy, Rahul=boy, Karen=girl, Arjun=boy, Aanchal=girl}
//Salman=boy
//Aanchal=girl