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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
| // NavigableSubMap是一个抽象类,继承了AbstractMap,实现了NavigableMap接口 static abstract class NavigableSubMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, java.io.Serializable { // 存储内容的Map final TreeMap<K,V> m; // lowKey、highKey final K lo, hi; // 标识map的边界是否是map的第一个节点和最后一个节点 final boolean fromStart, toEnd; // 是否包含最低lo、最高位置hi final boolean loInclusive, hiInclusive; // 通过上面的三组变量可以组成两个三元组表示一个集合的两个端点 // 构造方法 NavigableSubMap(TreeMap<K,V> m, boolean fromStart, K lo, boolean loInclusive, boolean toEnd, K hi, boolean hiInclusive) { if (!fromStart && !toEnd) { // lo>hi抛出异常 if (m.compare(lo, hi) > 0) throw new IllegalArgumentException("fromKey > toKey"); } else { if (!fromStart) // type check m.compare(lo, lo); if (!toEnd) m.compare(hi, hi); }
this.m = m; this.fromStart = fromStart; this.lo = lo; this.loInclusive = loInclusive; this.toEnd = toEnd; this.hi = hi; this.hiInclusive = hiInclusive; }
// tooLow 判断传入的key是否太小 final boolean tooLow(Object key) { // 如果fromStart为false,需要判断最低边界 if (!fromStart) { int c = m.compare(key, lo); // 如果key<lo或者相等但是map的边界不包含lo,那么key越界了,即小于最小值 if (c < 0 || (c == 0 && !loInclusive)) return true; } // 默认返回false return false; } // 与上面的tooLow类似 final boolean tooHigh(Object key) { if (!toEnd) { int c = m.compare(key, hi); if (c > 0 || (c == 0 && !hiInclusive)) return true; } return false; } // 判断是否在范围内,即满足最低最高限制,结合tooLow和tooHigh即可 final boolean inRange(Object key) { return !tooLow(key) && !tooHigh(key); } // 是否在封闭的区间内 final boolean inClosedRange(Object key) { return (fromStart || m.compare(key, lo) >= 0) && (toEnd || m.compare(hi, key) >= 0); } // 判断是否是在一个区间内 final boolean inRange(Object key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } // 获取绝对的最低的节点 final TreeMap.Entry<K,V> absLowest() { /* 如果fromStart为true,获取第一个节点,否则根据loInclusive是否为true,即是否包含lo来决定获取Ceiling节点或Higher节点;getCeilingEntry意为获取指定key的节点或者比指定key大的最小节点,如果不存在则返回null;getHigherEntry意为获取比指定key大的最小节点,如果不存在,返回null */ TreeMap.Entry<K,V> e = (fromStart ? m.getFirstEntry() : (loInclusive ? m.getCeilingEntry(lo) : m.getHigherEntry(lo))); // 判断得到的节点是否为空或者key过大 return (e == null || tooHigh(e.key)) ? null : e; } // 与absLowest类似 final TreeMap.Entry<K,V> absHighest() { TreeMap.Entry<K,V> e = (toEnd ? m.getLastEntry() : (hiInclusive ? m.getFloorEntry(hi) : m.getLowerEntry(hi))); return (e == null || tooLow(e.key)) ? null : e; } // 寻找大于等于key的最小的节点 final TreeMap.Entry<K,V> absCeiling(K key) { // 如果key太小,返回绝对的最小的节点 if (tooLow(key)) return absLowest(); // 获取允许的key的极限节点(满足要求的最小的节点) TreeMap.Entry<K,V> e = m.getCeilingEntry(key); return (e == null || tooHigh(e.key)) ? null : e; } // 和absCeiling类似,只是获取的不包含相等的情况,而是寻找大于key的最小节点 final TreeMap.Entry<K,V> absHigher(K key) { if (tooLow(key)) return absLowest(); TreeMap.Entry<K,V> e = m.getHigherEntry(key); return (e == null || tooHigh(e.key)) ? null : e; } // 获取绝对的小于等于key的节点 final TreeMap.Entry<K,V> absFloor(K key) { // 指定的key超出了hi,直接返回绝对的允许的最大的节点 if (tooHigh(key)) return absHighest(); /* getFloorEntry 获取的是指定key的节点,如果不存在这样的节点,就去获取比指定key小的最大的节点,如果这样的节点也不存在,返回null*/ TreeMap.Entry<K,V> e = m.getFloorEntry(key); return (e == null || tooLow(e.key)) ? null : e; } // 与上面的absFloor类似,只是不包含等于的情况 final TreeMap.Entry<K,V> absLower(K key) { if (tooHigh(key)) return absHighest(); TreeMap.Entry<K,V> e = m.getLowerEntry(key); return (e == null || tooLow(e.key)) ? null : e; }
// 返回比最大的节点“还要大”的节点(Fence是栅栏、围栏的意思) final TreeMap.Entry<K,V> absHighFence() { /* 如果toEnd是true,那么“围在”它外面的是null,如果是false,根据hi是否被包含返回getHigherEntry或getCeilingEntry,这两个方法意思在上面的方法中说明过了 */ return (toEnd ? null : (hiInclusive ? m.getHigherEntry(hi) : m.getCeilingEntry(hi))); }
// 与absHighFence类似 final TreeMap.Entry<K,V> absLowFence() { return (fromStart ? null : (loInclusive ? m.getLowerEntry(lo) : m.getFloorEntry(lo))); }
abstract TreeMap.Entry<K,V> subLowest(); abstract TreeMap.Entry<K,V> subHighest(); abstract TreeMap.Entry<K,V> subCeiling(K key); abstract TreeMap.Entry<K,V> subHigher(K key); abstract TreeMap.Entry<K,V> subFloor(K key); abstract TreeMap.Entry<K,V> subLower(K key); abstract Iterator<K> keyIterator(); abstract Iterator<K> descendingKeyIterator();
// 如果fromStart、toEnd都是true,那么判断空、获取大小都是直接通过m,不然就必须使用entrySet()先获取节点集 public boolean isEmpty() { return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty(); }
public int size() { return (fromStart && toEnd) ? m.size() : entrySet().size(); } // 判断是否存在key先判断范围,在通过TreeMap的containKey方法来判断 public final boolean containsKey(Object key) { return inRange(key) && m.containsKey(key); } // 添加节点 public final V put(K key, V value) { // 判断要添加的key是否在范围内 if (!inRange(key)) throw new IllegalArgumentException("key out of range"); return m.put(key, value); } public final V get(Object key) { return !inRange(key)? null : m.get(key); } public final V remove(Object key) { return !inRange(key)? null : m.remove(key); } public final Map.Entry<K,V> ceilingEntry(K key) { /* exportEntry(TreeMap.Entry<K,V> e)方法返回的是Map.Entry<K,V>对象,它的Key 和Value和传入的节点的Key 和Value相同 */ return exportEntry(subCeiling(key)); } public final K ceilingKey(K key) { // keyOrNull根据传入的节点是否为null返回null或节点的key(相当于提供了一个null安全的获取key的方法) return keyOrNull(subCeiling(key)); } public final Map.Entry<K,V> higherEntry(K key) { return exportEntry(subHigher(key)); } public final K higherKey(K key) { return keyOrNull(subHigher(key)); } public final Map.Entry<K,V> floorEntry(K key) { return exportEntry(subFloor(key)); } public final K floorKey(K key) { return keyOrNull(subFloor(key)); } public final Map.Entry<K,V> lowerEntry(K key) { return exportEntry(subLower(key)); } public final K lowerKey(K key) { return keyOrNull(subLower(key)); } public final K firstKey() { return key(subLowest()); } public final K lastKey() { return key(subHighest()); } public final Map.Entry<K,V> firstEntry() { return exportEntry(subLowest()); } public final Map.Entry<K,V> lastEntry() { return exportEntry(subHighest()); } // 返回并删除第一个节点 public final Map.Entry<K,V> pollFirstEntry() { TreeMap.Entry<K,V> e = subLowest(); Map.Entry<K,V> result = exportEntry(e); if (e != null) m.deleteEntry(e); return result; } // 返回并删除最后一个节点 public final Map.Entry<K,V> pollLastEntry() { TreeMap.Entry<K,V> e = subHighest(); Map.Entry<K,V> result = exportEntry(e); if (e != null) m.deleteEntry(e); return result; }
// 这些都是视图 transient NavigableMap<K,V> descendingMapView = null; transient EntrySetView entrySetView = null; transient KeySet<K> navigableKeySetView = null; // 返回TreeMap的KeySet public final NavigableSet<K> navigableKeySet() { KeySet<K> nksv = navigableKeySetView; return (nksv != null) ? nksv : (navigableKeySetView = new TreeMap.KeySet(this)); } public final Set<K> keySet() { return navigableKeySet(); } // 逆序的KeySet public NavigableSet<K> descendingKeySet() { return descendingMap().navigableKeySet(); } // 返回一个子Map public final SortedMap<K,V> subMap(K fromKey, K toKey) { return subMap(fromKey, true, toKey, false); } // 下面这几个方法会在后面给出分析 public final SortedMap<K,V> headMap(K toKey) { return headMap(toKey, false); } public final SortedMap<K,V> tailMap(K fromKey) { return tailMap(fromKey, true); }
// 视图类 abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> { private transient int size = -1, sizeModCount; // 返回子Map的大小 public int size() { // 如果fromStart和toEnd都是true,返回的是m的size if (fromStart && toEnd) return m.size(); // size=-1或标记size不同,重新计算一次size if (size == -1 || sizeModCount != m.modCount) { sizeModCount = m.modCount; size = 0; Iterator i = iterator(); while (i.hasNext()) { size++; i.next(); } } return size; } // 判断EntrySet是否为空 public boolean isEmpty() { TreeMap.Entry<K,V> n = absLowest(); return n == null || tooHigh(n.key); } // 判断是否包含某个对象 public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<K,V> entry = (Map.Entry<K,V>) o; K key = entry.getKey(); // key不在范围内,返回false if (!inRange(key)) return false; // 判断是否有键和值如传入节点的键和值相等的节点 TreeMap.Entry node = m.getEntry(key); return node != null && valEquals(node.getValue(), entry.getValue()); } // 移除一个节点 public boolean remove(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<K,V> entry = (Map.Entry<K,V>) o; K key = entry.getKey(); if (!inRange(key)) return false; TreeMap.Entry<K,V> node = m.getEntry(key); // 找到节点并移除,返回true if (node!=null && valEquals(node.getValue(),entry.getValue())){ m.deleteEntry(node); return true; } return false; } }
//子类迭代器 abstract class SubMapIterator<T> implements Iterator<T> { // 上一次被返回的节点 TreeMap.Entry<K,V> lastReturned; // 下一个节点 TreeMap.Entry<K,V> next; // “栅栏”key(如果是向大的方向遍历,不能访问key大于等于fenceKey的节点;如果是向小的方向遍历,不能访问key小于等于fenceKey的节点) final K fenceKey; int expectedModCount; // 构造方法 SubMapIterator(TreeMap.Entry<K,V> first, TreeMap.Entry<K,V> fence) { expectedModCount = m.modCount; lastReturned = null; next = first; fenceKey = fence == null ? null : fence.key; } // 判断是否还有下一个节点 public final boolean hasNext() { // 与普通的hasNext的判断不同的是这里必须判断next的key是否超出了fenceKey return next != null && next.key != fenceKey; } // 获得下一个节点的方法,比较容易理解 final TreeMap.Entry<K,V> nextEntry() { TreeMap.Entry<K,V> e = next; if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); next = successor(e); lastReturned = e; return e; } // 另一种遍历方法,向前遍历 final TreeMap.Entry<K,V> prevEntry() { TreeMap.Entry<K,V> e = next; if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); next = predecessor(e); lastReturned = e; return e; } // 删除节点后可以继续遍历剩余的节点,因为删除前用next保留了lastReturned节点,而这个节点在删除操作的过程中被替换成了它的继承者 final void removeAscending() { if (lastReturned == null) throw new IllegalStateException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned.left != null && lastReturned.right != null) // next指向lastReturned所指向的节点,这个节点的内容在删除lastReturned的时候被改变了 next = lastReturned; m.deleteEntry(lastReturned); lastReturned = null; expectedModCount = m.modCount; } // 删除之后next指向的节点其实被删除了,不能继续迭代访问 final void removeDescending() { if (lastReturned == null) throw new IllegalStateException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); m.deleteEntry(lastReturned); lastReturned = null; expectedModCount = m.modCount; }
} //下面的几个内部类很简单,都是对SubMapIterator的调用或间接调用,不再解释 final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> { SubMapEntryIterator(TreeMap.Entry<K,V> first, TreeMap.Entry<K,V> fence) { super(first, fence); } public Map.Entry<K,V> next() { return nextEntry(); } public void remove() { removeAscending(); } }
final class SubMapKeyIterator extends SubMapIterator<K> { SubMapKeyIterator(TreeMap.Entry<K,V> first, TreeMap.Entry<K,V> fence) { super(first, fence); } public K next() { return nextEntry().key; } public void remove() { removeAscending(); } }
final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> { DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last, TreeMap.Entry<K,V> fence) { super(last, fence); }
public Map.Entry<K,V> next() { return prevEntry(); } public void remove() { removeDescending(); } }
final class DescendingSubMapKeyIterator extends SubMapIterator<K> { DescendingSubMapKeyIterator(TreeMap.Entry<K,V> last, TreeMap.Entry<K,V> fence) { super(last, fence); } public K next() { return prevEntry().key; } public void remove() { removeDescending(); } } }
|