-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedMapDemo.java
More file actions
40 lines (30 loc) · 1.03 KB
/
Copy pathSortedMapDemo.java
File metadata and controls
40 lines (30 loc) · 1.03 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
package MapPackage;
import java.util.Iterator;
import java.util.TreeMap;
public class SortedMapDemo {
public static void main(String[] args) {
//TreeMap是现唯一实现可排序的Map,key必须实现Compare接口
TreeMap<Integer, String> sortedMap = new TreeMap<>(new CountingMapData(10));
System.out.println(sortedMap);
Integer low = sortedMap.firstKey();
Integer high = sortedMap.lastKey();
System.out.println(low);
System.out.println(high);
Iterator<Integer> it = sortedMap.keySet().iterator();
for (int i = 0; i < 6; i++) {
if (i == 3) {
low = it.next();
}
if (i == 6) {
high = it.next();
}
it.next();
}
System.out.println(low);
System.out.println(high);
//生成子集
System.out.println(sortedMap.subMap(low, high));
System.out.println(sortedMap.headMap(high));
System.out.println(sortedMap.tailMap(low));
}
}