-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathTreeSet源码解析.md
284 lines (202 loc) · 5.38 KB
/
TreeSet源码解析.md
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#### TreeSet

- NavigableSet 定义了一些有方向的操作

##### 数据结构
```java
private transient NavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
```
##### 构造方法
```java
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
public TreeSet() {
this(new TreeMap<>());
}
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
public TreeSet(Collection<? extends E> c) {
this();
// 批量添加
addAll(c);
}
public TreeSet(SortedSet<E> s) {
this(s.comparator());
// 批量添加
addAll(s);
}
```
##### 添加元素
- 单个元素
```java
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
```
- 多个元素
```java
public boolean addAll(Collection<? extends E> c) {
// Use linear-time version if applicable
// 情况一
if (m.size()==0 && c.size() > 0 &&
c instanceof SortedSet &&
m instanceof TreeMap) {
SortedSet<? extends E> set = (SortedSet<? extends E>) c;
TreeMap<E,Object> map = (TreeMap<E, Object>) m;
if (Objects.equals(set.comparator(), map.comparator())) {
map.addAllForTreeSet(set, PRESENT);
return true;
}
}
// 情况二
return super.addAll(c);
}
```
##### 删除元素
- 单个元素
```java
public boolean remove(Object o) {
return m.remove(o)==PRESENT;
}
```
##### 查找元素
- 是否包含
```java
public boolean contains(Object o) {
return m.containsKey(o);
}
```
- 接近的元素
```java
public E lower(E e) {
return m.lowerKey(e);
}
public E floor(E e) {
return m.floorKey(e);
}
public E ceiling(E e) {
return m.ceilingKey(e);
}
public E higher(E e) {
return m.higherKey(e);
}
```
- 首尾的元素
```java
public E first() {
return m.firstKey();
}
public E last() {
return m.lastKey();
}
/**
* 拓展方法
*/
public E pollFirst() {
Map.Entry<E,?> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
public E pollLast() {
Map.Entry<E,?> e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
```
##### 清空
```java
public void clear() {
m.clear();
}
```
##### 迭代器
```java
public Iterator<E> iterator() { // 正序 Iterator 迭代器
return m.navigableKeySet().iterator();
}
public Iterator<E> descendingIterator() { // 倒序 Iterator 迭代器
return m.descendingKeySet().iterator();
}
```
##### 序列化
- 序列化
```java
@java.io.Serial
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden stuff
// 写入非静态属性、非 transient 属性
s.defaultWriteObject();
// Write out Comparator
// 写入比较器
s.writeObject(m.comparator());
// Write out size
// 写入 key-value 键值对数量
s.writeInt(m.size());
// Write out all elements in the proper order.
// 写入具体的 key-value 键值对
for (E e : m.keySet())
s.writeObject(e);
}
```
- 反序列化
```java
@java.io.Serial
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden stuff
// 读取非静态属性、非 transient 属性
s.defaultReadObject();
// Read in Comparator
// 读取比较器
@SuppressWarnings("unchecked")
Comparator<? super E> c = (Comparator<? super E>) s.readObject();
// Create backing TreeMap
// 创建 TreeMap 对象
TreeMap<E,Object> tm = new TreeMap<>(c);
m = tm;
// Read in size
// 读取 key-value 键值对数量
int size = s.readInt();
// 读取具体的 key-value 键值对
tm.readTreeSet(size, s, PRESENT);
}
```
```java
// TreeMap.java
void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal)
throws java.io.IOException, ClassNotFoundException {
buildFromSorted(size, null, s, defaultVal);
}
```
##### 查找范围
(对 NavigableSet 的实现)
```java
// subSet 组
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
E toElement, boolean toInclusive) {
return new TreeSet<>(m.subMap(fromElement, fromInclusive,
toElement, toInclusive));
}
public SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
// headSet 组
public NavigableSet<E> headSet(E toElement, boolean inclusive) {
return new TreeSet<>(m.headMap(toElement, inclusive));
}
public SortedSet<E> headSet(E toElement) {
return headSet(toElement, false);
}
// tailSet 组
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new TreeSet<>(m.tailMap(fromElement, inclusive));
}
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
```
##### 小结
TreeSet 是基于 TreeMap 的 Set 实现类