forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigableMapDemo.java
More file actions
39 lines (34 loc) · 1.23 KB
/
Copy pathNavigableMapDemo.java
File metadata and controls
39 lines (34 loc) · 1.23 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
import java.util.Iterator;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.TreeMap;
public class NavigableMapDemo{
public static void main(String[] args){
NavigableMap<String, Integer> nm = new TreeMap<String, Integer>();
String[] birds = {"sparrow", "bluejay", "robin"};
int[] ints = {83, 12, 19};
for(int i=0; i<birds.length; i++){
nm.put(birds[i], ints[i]);
}
System.out.println("Map = " + nm);
System.out.print("Ascending order of keys: ");
NavigableSet<String> keySet = nm.navigableKeySet();
for(String str: keySet){
System.out.print(str + " ");
}
System.out.println();
System.out.print("Descending order of keys: ");
for(String str: nm.descendingKeySet()){
System.out.print(str + " ");
}
System.out.println();
System.out.println("First entry = " + nm.firstEntry());
System.out.println("Last entry = " + nm.lastEntry());
System.out.println("Entry < ostrich is " + nm.lowerEntry("ostrich"));
System.out.println("Entry > crow is " + nm.higherEntry("crow"));
System.out.println("Poll first entry: " + nm.pollFirstEntry());
System.out.println("Map = " + nm);
System.out.println("Poll last entry: " + nm.pollLastEntry());
System.out.println("Map = " + nm);
}
}