Repository navigation
Expand file tree
/
Copy pathCode02_TreeSetAndTreeMap.java
More file actions
73 lines (61 loc) · 1.91 KB
/
Copy pathCode02_TreeSetAndTreeMap.java
File metadata and controls
73 lines (61 loc) · 1.91 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
package class026;
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.TreeSet;
public class Code02_TreeSetAndTreeMap {
public static void main(String[] args) {
// 底层红黑树
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(5, "这是5");
treeMap.put(7, "这是7");
treeMap.put(1, "这是1");
treeMap.put(2, "这是2");
treeMap.put(3, "这是3");
treeMap.put(4, "这是4");
treeMap.put(8, "这是8");
System.out.println(treeMap.containsKey(1));
System.out.println(treeMap.containsKey(10));
System.out.println(treeMap.get(4));
treeMap.put(4, "张三是4");
System.out.println(treeMap.get(4));
treeMap.remove(4);
System.out.println(treeMap.get(4) == null);
System.out.println(treeMap.firstKey());
System.out.println(treeMap.lastKey());
// TreeMap中,所有的key,<= 4且最近的key是什么
System.out.println(treeMap.floorKey(4));
// TreeMap中,所有的key,>= 4且最近的key是什么
System.out.println(treeMap.ceilingKey(4));
System.out.println("========");
TreeSet<Integer> set = new TreeSet<>();
set.add(3);
set.add(3);
set.add(4);
set.add(4);
System.out.println("有序表大小 : " + set.size());
while (!set.isEmpty()) {
System.out.println(set.pollFirst());
// System.out.println(set.pollLast());
}
// 堆,默认小根堆、如果要大根堆,定制比较器!
PriorityQueue<Integer> heap1 = new PriorityQueue<>();
heap1.add(3);
heap1.add(3);
heap1.add(4);
heap1.add(4);
System.out.println("堆大小 : " + heap1.size());
while (!heap1.isEmpty()) {
System.out.println(heap1.poll());
}
// 定制的大根堆,用比较器!
PriorityQueue<Integer> heap2 = new PriorityQueue<>((a, b) -> b - a);
heap2.add(3);
heap2.add(3);
heap2.add(4);
heap2.add(4);
System.out.println("堆大小 : " + heap2.size());
while (!heap2.isEmpty()) {
System.out.println(heap2.poll());
}
}
}