Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1213e4

Browse files
author
Rajeev Kumar Singh
committed
More examples
1 parent d5c5b47 commit c1213e4

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

‎java-treemap-examples/src/AccessRemoveEntriesFromTreeMapExample.java‎ renamed to ‎java-treemap-examples/src/AccessEntriesFromTreeMapExample.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import java.util.Map;
22
import java.util.TreeMap;
33

4-
public class AccessRemoveEntriesFromTreeMapExample {
4+
public class AccessEntriesFromTreeMapExample {
55
public static void main(String[] args) {
66
TreeMap<Integer, String> employees = new TreeMap<>();
7+
78
employees.put(1003, "Rajeev");
89
employees.put(1001, "James");
910
employees.put(1002, "Sachin");
@@ -24,19 +25,16 @@ public static void main(String[] args) {
2425
System.out.println("Employee does not exist with id : " + id);
2526
}
2627

28+
// Find the first and last entry
29+
System.out.println("First entry in employees map : " + employees.firstEntry());
30+
System.out.println("Last entry in employees map : " + employees.lastEntry());
31+
2732
// Find the entry whose key is just less than the given key
2833
Map.Entry<Integer, String> employeeJustBelow = employees.lowerEntry(1002);
2934
System.out.println("Employee just below id 1002 : " + employeeJustBelow);
3035

3136
// Find the entry whose key is just higher than the given key
3237
Map.Entry<Integer, String> employeeJustAbove = employees.higherEntry(1002);
3338
System.out.println("Employee just above id 1002 : " + employeeJustAbove);
34-
35-
System.out.println("First entry in employees map : " + employees.firstEntry());
36-
System.out.println("Last entry in employees map : " + employees.lastEntry());
37-
38-
// Removing a key from the TreeMap
39-
String name = employees.remove(1003);
40-
System.out.println("Removed employee (1003 : " + name + "), New TreeMap " + employees);
4139
}
4240
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Map;
2+
import java.util.TreeMap;
3+
4+
public class RemoveEntriesFromTreeMapExample {
5+
public static void main(String[] args) {
6+
TreeMap<String, String> countryISOCodeMapping = new TreeMap<>();
7+
8+
countryISOCodeMapping.put("India", "IN");
9+
countryISOCodeMapping.put("United States of America", "US");
10+
countryISOCodeMapping.put("China", "CN");
11+
countryISOCodeMapping.put("United Kingdom", "UK");
12+
countryISOCodeMapping.put("Russia", "RU");
13+
countryISOCodeMapping.put("Japan", "JP");
14+
15+
System.out.println("CountryISOCodeMapping : " + countryISOCodeMapping);
16+
17+
// Remove the mapping for a given key
18+
String countryName = "Japan";
19+
String isoCode = countryISOCodeMapping.remove(countryName);
20+
if(isoCode != null) {
21+
System.out.println("Removed (" + countryName + " => " + isoCode + ") from the TreeMap. New TreeMap " + countryISOCodeMapping);
22+
} else {
23+
System.out.println(countryName + " does not exist, or it is mapped to a null value");
24+
}
25+
26+
// Remove the mapping for a given key only if it is mapped to a given value
27+
countryName = "India";
28+
boolean isRemoved = countryISOCodeMapping.remove(countryName, "IA");
29+
System.out.println("Was the mapping removed for " + countryName + "? : " + isRemoved);
30+
31+
// Remove the first entry from the TreeMap
32+
Map.Entry<String, String> firstEntry = countryISOCodeMapping.pollFirstEntry();
33+
System.out.println("Removed firstEntry : " + firstEntry + ", New TreeMap : " + countryISOCodeMapping);
34+
35+
// Remove the last entry from the TreeMap
36+
Map.Entry<String, String> lastEntry = countryISOCodeMapping.pollLastEntry();
37+
System.out.println("Removed lastEntry : " + lastEntry + ", New TreeMap : " + countryISOCodeMapping);
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /