-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjava-Tree-Map examples
More file actions
147 lines (117 loc) · 6.02 KB
/
java-Tree-Map examples
File metadata and controls
147 lines (117 loc) · 6.02 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.Map;
import java.util.TreeMap;
public class AccessEntriesFromTreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> employees = new TreeMap<>();
employees.put(1003, "Rajeev");
employees.put(1001, "James");
employees.put(1002, "Sachin");
employees.put(1004, "Chris");
System.out.println("Employees map : " + employees);
// Finding the size of a TreeMap
System.out.println("Total number of employees : " + employees.size());
// Check if a given key exists in a TreeMap
Integer id = 1004;
if(employees.containsKey(id)) {
// Get the value associated with a given key in a TreeMap
String name = employees.get(id);
System.out.println("Employee with id " + id + " : " + name);
} else {
System.out.println("Employee does not exist with id : " + id);
}
// Find the first and last entry
System.out.println("First entry in employees map : " + employees.firstEntry());
System.out.println("Last entry in employees map : " + employees.lastEntry());
// Find the entry whose key is just less than the given key
Map.Entry<Integer, String> employeeJustBelow = employees.lowerEntry(1002);
System.out.println("Employee just below id 1002 : " + employeeJustBelow);
// Find the entry whose key is just higher than the given key
Map.Entry<Integer, String> employeeJustAbove = employees.higherEntry(1002);
System.out.println("Employee just above id 1002 : " + employeeJustAbove);
}
}
***********************************************************************************************************************************************************************
import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
public class CreateTreeMapCaseInsensitiveOrderExample {
public static void main(String[] args) {
// TreeMap with keys sorted by ignoring case
SortedMap<String, String> fileExtensions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
/*
The above statement is the short form of -
SortedMap<String, String> fileExtensions = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
*/
fileExtensions.put("PYTHON", ".py");
fileExtensions.put("c++", ".cpp");
fileExtensions.put("KOTLIN", ".kt");
fileExtensions.put("Golang", ".go");
// The keys will be sorted ignoring the case (Try removing String.CASE_INSENSITIVE_ORDER and see the output)
System.out.println(fileExtensions);
}
}
***********************************************************************************************************************************************************************
import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
public class CreateTreeMapCustomComparatorExample {
public static void main(String[] args) {
// Creating a TreeMap with a Custom comparator (Descending order)
SortedMap<String, String> fileExtensions = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
});
/*
The above TreeMap with custom Comparator can be simply written as -
SortedMap<String, String> fileExtensions = new TreeMap<>(Comparator.reverseOrder());
*/
// Adding new key-value pairs to a TreeMap
fileExtensions.put("python", ".py");
fileExtensions.put("c++", ".cpp");
fileExtensions.put("kotlin", ".kt");
fileExtensions.put("golang", ".go");
fileExtensions.put("java", ".java");
// Printing the TreeMap (The keys will be sorted based on the supplied comparator)
System.out.println(fileExtensions);
}
}
***********************************************************************************************************************************************************************
import java.util.Map;
import java.util.TreeMap;
public class RemoveEntriesFromTreeMapExample {
public static void main(String[] args) {
TreeMap<String, String> countryISOCodeMapping = new TreeMap<>();
countryISOCodeMapping.put("India", "IN");
countryISOCodeMapping.put("United States of America", "US");
countryISOCodeMapping.put("China", "CN");
countryISOCodeMapping.put("United Kingdom", "UK");
countryISOCodeMapping.put("Russia", "RU");
countryISOCodeMapping.put("Japan", "JP");
System.out.println("CountryISOCodeMapping : " + countryISOCodeMapping);
// Remove the mapping for a given key
String countryName = "Japan";
String isoCode = countryISOCodeMapping.remove(countryName);
if(isoCode != null) {
System.out.println("Removed (" + countryName + " => " + isoCode + ") from the TreeMap. New TreeMap " + countryISOCodeMapping);
} else {
System.out.println(countryName + " does not exist, or it is mapped to a null value");
}
// Remove the mapping for the given key only if it is mapped to the given value
countryName = "India";
boolean isRemoved = countryISOCodeMapping.remove(countryName, "IA");
System.out.println("Was the mapping removed for " + countryName + "? : " + isRemoved);
// Remove the first entry from the TreeMap
Map.Entry<String, String> firstEntry = countryISOCodeMapping.pollFirstEntry();
System.out.println("Removed firstEntry : " + firstEntry + ", New TreeMap : " + countryISOCodeMapping);
// Remove the last entry from the TreeMap
Map.Entry<String, String> lastEntry = countryISOCodeMapping.pollLastEntry();
System.out.println("Removed lastEntry : " + lastEntry + ", New TreeMap : " + countryISOCodeMapping);
}
}