对NavigableMap接口的实现


firstKey()、lastKey()、lowerKey()、higherKey()、ceilingKey()、floorKey();
firstEntry()、 lastEntry()、 lowerEntry()、 higherEntry()、 floorEntry()、 ceilingEntry()、 pollFirstEntry() 、 pollLastEntry();
上面已经讲解过这些API了,下面对其它的API进行说明。

1 反向TreeMap
descendingMap() 的作用是返回当前TreeMap的反向的TreeMap。所谓反向,就是排序顺序和原始的顺序相反。

我们已经知道TreeMap是一颗红黑树,而红黑树是有序的。
TreeMap的排序方式是通过比较器,在创建TreeMap的时候,若指定了比较器,则使用该比较器;否则,就使用Java的默认比较器。
而获取TreeMap的反向TreeMap的原理就是将比较器反向即可!

理解了descendingMap()的反向原理之后,再讲解一下descendingMap()的代码。


  1. // 获取TreeMap的降序Map  
  2. public NavigableMap<K, V> descendingMap() {  
  3.     NavigableMap<K, V> km = descendingMap;  
  4.     return (km != null) ? km :  
  5.         (descendingMap = new DescendingSubMap(this,  
  6.                                               true, null, true,  
  7.                                               true, null, true));  
  8. }  


从中,我们看出descendingMap()实际上是返回DescendingSubMap类的对象。下面,看看DescendingSubMap的源码:

1. static final class DescendingSubMap<K,V>  extends NavigableSubMap<K,V> {  
2.     private static final long serialVersionUID = 912986545866120460L;  
3.     DescendingSubMap(TreeMap<K,V> m,  
4.                     boolean fromStart, K lo, boolean loInclusive,  
5.                     boolean toEnd,     K hi, boolean hiInclusive) {  
6.         super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);  
7.     }  
8.  
9.     // 反转的比较器:是将原始比较器反转得到的。  
10.     private final Comparator<? super K> reverseComparator =  
11.         Collections.reverseOrder(m.comparator);  
12.  
13.     // 获取反转比较器  
14.     public Comparator<? super K> comparator() {  
15.         return reverseComparator;  
16.     }  
17.  
18.     // 获取“子Map”。  
19.     // 范围是从fromKey 到 toKey;fromInclusive是是否包含fromKey的标记,toInclusive是是否包含toKey的标记  
20.     public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,  
21.                                     K toKey,   boolean toInclusive) {  
22.         if (!inRange(fromKey, fromInclusive))  
23.             throw new IllegalArgumentException("fromKey out of range");  
24.         if (!inRange(toKey, toInclusive))  
25.             throw new IllegalArgumentException("toKey out of range");  
26.         return new DescendingSubMap(m,  
27.                                     false, toKey,   toInclusive,  
28.                                     false, fromKey, fromInclusive);  
29.     }  
30.  
31.     // 获取“Map的头部”。  
32.     // 范围从第一个节点 到 toKey, inclusive是是否包含toKey的标记  
33.     public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {  
34.         if (!inRange(toKey, inclusive))  
35.             throw new IllegalArgumentException("toKey out of range");  
36.         return new DescendingSubMap(m,  
37.                                     false, toKey, inclusive,  
38.                                     toEnd, hi,    hiInclusive);  
39.     }  
40.  
41.     // 获取“Map的尾部”。  
42.     // 范围是从 fromKey 到 最后一个节点,inclusive是是否包含fromKey的标记  
43.     public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive){  
44.         if (!inRange(fromKey, inclusive))  
45.             throw new IllegalArgumentException("fromKey out of range");  
46.         return new DescendingSubMap(m,  
47.                                     fromStart, lo, loInclusive,  
48.                                     false, fromKey, inclusive);  
49.     }  
50.  
51.     // 获取对应的降序Map  
52.     public NavigableMap<K,V> descendingMap() {  
53.         NavigableMap<K,V> mv = descendingMapView;  
54.         return (mv != null) ? mv :  
55.             (descendingMapView =  
56.              new AscendingSubMap(m,  
57.                                  fromStart, lo, loInclusive,  
58.                                  toEnd,     hi, hiInclusive));  
59.     }  
60.  
61.     // 返回“升序Key迭代器”  
62.     Iterator<K> keyIterator() {  
63.         return new DescendingSubMapKeyIterator(absHighest(), absLowFence());  
64.     }  
65.  
66.     // 返回“降序Key迭代器”  
67.     Iterator<K> descendingKeyIterator() {  
68.         return new SubMapKeyIterator(absLowest(), absHighFence());  
69.     }  
70.  
71.     // “降序EntrySet集合”类  
72.     // 实现了iterator()  
73.     final class DescendingEntrySetView extends EntrySetView {  
74.         public Iterator<Map.Entry<K,V>> iterator() {  
75.             return new DescendingSubMapEntryIterator(absHighest(), absLowFence());  
76.         }  
77.     }  
78.  
79.     // 返回“降序EntrySet集合”  
80.     public Set<Map.Entry<K,V>> entrySet() {  
81.         EntrySetView es = entrySetView;  
82.         return (es != null) ? es : new DescendingEntrySetView();  
83.     }  
84.  
85.     TreeMap.Entry<K,V> subLowest()       { return absHighest(); }  
86.     TreeMap.Entry<K,V> subHighest()      { return absLowest(); }  
87.     TreeMap.Entry<K,V> subCeiling(K key) { return absFloor(key); }  
88.     TreeMap.Entry<K,V> subHigher(K key)  { return absLower(key); }  
89.     TreeMap.Entry<K,V> subFloor(K key)   { return absCeiling(key); }  
90.     TreeMap.Entry<K,V> subLower(K key)   { return absHigher(key); }  
91. } 
 
从中,我们看出DescendingSubMap是降序的SubMap,它的实现机制是将“SubMap的比较器反转”。
它继承于NavigableSubMap。而NavigableSubMap是一个继承于AbstractMap的抽象类;它包括2个子类——"(升序)AscendingSubMap"和"(降序)DescendingSubMap"。NavigableSubMap为它的两个子类实现了许多公共API。
下面看看NavigableSubMap的源码。 
1. static abstract class NavigableSubMap<K,V> extends AbstractMap<K,V>  
2.     implements NavigableMap<K,V>, java.io.Serializable {  
3.     // TreeMap的拷贝  
4.     final TreeMap<K,V> m;  
5.     // lo是“子Map范围的最小值”,hi是“子Map范围的最大值”;  
6.     // loInclusive是“是否包含lo的标记”,hiInclusive是“是否包含hi的标记”  
7.     // fromStart是“表示是否从第一个节点开始计算”,  
8.     // toEnd是“表示是否计算到最后一个节点      ”  
9.     final K lo, hi;        
10.     final boolean fromStart, toEnd;  
11.     final boolean loInclusive, hiInclusive;  
12.  
13.     // 构造函数  
14.     NavigableSubMap(TreeMap<K,V> m,  
15.                     boolean fromStart, K lo, boolean loInclusive,  
16.                     boolean toEnd,     K hi, boolean hiInclusive) {  
17.         if (!fromStart && !toEnd) {  
18.             if (m.compare(lo, hi) > 0)  
19.                 throw new IllegalArgumentException("fromKey > toKey");  
20.         } else {  
21.             if (!fromStart) // type check  
22.                 m.compare(lo, lo);  
23.             if (!toEnd)  
24.                 m.compare(hi, hi);  
25.         }  
26.  
27.         this.m = m;  
28.         this.fromStart = fromStart;  
29.         this.lo = lo;  
30.         this.loInclusive = loInclusive;  
31.         this.toEnd = toEnd;  
32.         this.hi = hi;  
33.         this.hiInclusive = hiInclusive;  
34.     }  
35.  
36.     // 判断key是否太小  
37.     final boolean tooLow(Object key) {  
38.         // 若该SubMap不包括“起始节点”,  
39.         // 并且,“key小于最小键(lo)”或者“key等于最小键(lo),但最小键却没包括在该SubMap内”  
40.         // 则判断key太小。其余情况都不是太小!  
41.         if (!fromStart) {  
42.             int c = m.compare(key, lo);  
43.             if (c < 0 || (c == 0 && !loInclusive))  
44.                 return true;  
45.         }  
46.         return false;  
47.     }  
48.  
49.     // 判断key是否太大  
50.     final boolean tooHigh(Object key) {  
51.         // 若该SubMap不包括“结束节点”,  
52.         // 并且,“key大于最大键(hi)”或者“key等于最大键(hi),但最大键却没包括在该SubMap内”  
53.         // 则判断key太大。其余情况都不是太大!  
54.         if (!toEnd) {  
55.             int c = m.compare(key, hi);  
56.             if (c > 0 || (c == 0 && !hiInclusive))  
57.                 return true;  
58.         }  
59.         return false;  
60.     }  
61.  
62.     // 判断key是否在“lo和hi”开区间范围内  
63.     final boolean inRange(Object key) {  
64.         return !tooLow(key) && !tooHigh(key);  
65.     }  
66.  
67.     // 判断key是否在封闭区间内  
68.     final boolean inClosedRange(Object key) {  
69.         return (fromStart || m.compare(key, lo) >= 0)  
70.             && (toEnd || m.compare(hi, key) >= 0);  
71.     }  
72.  
73.     // 判断key是否在区间内, inclusive是区间开关标志  
74.     final boolean inRange(Object key, boolean inclusive) {  
75.         return inclusive ? inRange(key) : inClosedRange(key);  
76.     }  
77.  
78.     // 返回最低的Entry  
79.     final TreeMap.Entry<K,V> absLowest() {  
80.     // 若“包含起始节点”,则调用getFirstEntry()返回第一个节点  
81.     // 否则的话,若包括lo,则调用getCeilingEntry(lo)获取大于/等于lo的最小的Entry;  
82.     //           否则,调用getHigherEntry(lo)获取大于lo的最小Entry  
83.     TreeMap.Entry<K,V> e =  
84.             (fromStart ?  m.getFirstEntry() :  
85.              (loInclusive ? m.getCeilingEntry(lo) :  
86.                             m.getHigherEntry(lo)));  
87.         return (e == null || tooHigh(e.key)) ? null : e;  
88.     }  
89.  
90.     // 返回最高的Entry  
91.     final TreeMap.Entry<K,V> absHighest() {  
92.     // 若“包含结束节点”,则调用getLastEntry()返回最后一个节点  
93.     // 否则的话,若包括hi,则调用getFloorEntry(hi)获取小于/等于hi的最大的Entry;  
94.     //           否则,调用getLowerEntry(hi)获取大于hi的最大Entry  
95.     TreeMap.Entry<K,V> e =  
96.     TreeMap.Entry<K,V> e =  
97.             (toEnd ?  m.getLastEntry() :  
98.              (hiInclusive ?  m.getFloorEntry(hi) :  
99.                              m.getLowerEntry(hi)));  
100.         return (e == null || tooLow(e.key)) ? null : e;  
101.     }  
102.  
103.     // 返回"大于/等于key的最小的Entry"  
104.     final TreeMap.Entry<K,V> absCeiling(K key) {  
105.         // 只有在“key太小”的情况下,absLowest()返回的Entry才是“大于/等于key的最小Entry”  
106.         // 其它情况下不行。例如,当包含“起始节点”时,absLowest()返回的是最小Entry了!  
107.         if (tooLow(key))  
108.             return absLowest();  
109.         // 获取“大于/等于key的最小Entry”  
110.     TreeMap.Entry<K,V> e = m.getCeilingEntry(key);  
111.         return (e == null || tooHigh(e.key)) ? null : e;  
112.     }  
113.  
114.     // 返回"大于key的最小的Entry"  
115.     final TreeMap.Entry<K,V> absHigher(K key) {  
116.         // 只有在“key太小”的情况下,absLowest()返回的Entry才是“大于key的最小Entry”  
117.         // 其它情况下不行。例如,当包含“起始节点”时,absLowest()返回的是最小Entry了,而不一定是“大于key的最小Entry”!  
118.         if (tooLow(key))  
119.             return absLowest();  
120.         // 获取“大于key的最小Entry”  
121.     TreeMap.Entry<K,V> e = m.getHigherEntry(key);  
122.         return (e == null || tooHigh(e.key)) ? null : e;  
123.     }  
124.  
125.     // 返回"小于/等于key的最大的Entry"  
126.     final TreeMap.Entry<K,V> absFloor(K key) {  
127.         // 只有在“key太大”的情况下,(absHighest)返回的Entry才是“小于/等于key的最大Entry”  
128.         // 其它情况下不行。例如,当包含“结束节点”时,absHighest()返回的是最大Entry了!  
129.         if (tooHigh(key))  
130.             return absHighest();  
131.     // 获取"小于/等于key的最大的Entry"  
132.     TreeMap.Entry<K,V> e = m.getFloorEntry(key);  
133.         return (e == null || tooLow(e.key)) ? null : e;  
134.     }  
135.  
136.     // 返回"小于key的最大的Entry"  
137.     final TreeMap.Entry<K,V> absLower(K key) {  
138.         // 只有在“key太大”的情况下,(absHighest)返回的Entry才是“小于key的最大Entry”  
139.         // 其它情况下不行。例如,当包含“结束节点”时,absHighest()返回的是最大Entry了,而不一定是“小于key的最大Entry”!  
140.         if (tooHigh(key))  
141.             return absHighest();  
142.     // 获取"小于key的最大的Entry"  
143.     TreeMap.Entry<K,V> e = m.getLowerEntry(key);  
144.         return (e == null || tooLow(e.key)) ? null : e;  
145.     }  
146.  
147.     // 返回“大于最大节点中的最小节点”,不存在的话,返回null  
148.     final TreeMap.Entry<K,V> absHighFence() {  
149.         return (toEnd ? null : (hiInclusive ?  
150.                                 m.getHigherEntry(hi) :  
151.                                 m.getCeilingEntry(hi)));  
152.     }  
153.  
154.     // 返回“小于最小节点中的最大节点”,不存在的话,返回null  
155.     final TreeMap.Entry<K,V> absLowFence() {  
156.         return (fromStart ? null : (loInclusive ?  
157.                                     m.getLowerEntry(lo) :  
158.                                     m.getFloorEntry(lo)));  
159.     }  
160.  
161.     // 下面几个abstract方法是需要NavigableSubMap的实现类实现的方法  
162.     abstract TreeMap.Entry<K,V> subLowest();  
163.     abstract TreeMap.Entry<K,V> subHighest();  
164.     abstract TreeMap.Entry<K,V> subCeiling(K key);  
165.     abstract TreeMap.Entry<K,V> subHigher(K key);  
166.     abstract TreeMap.Entry<K,V> subFloor(K key);  
167.     abstract TreeMap.Entry<K,V> subLower(K key);  
168.     // 返回“顺序”的键迭代器  
169.     abstract Iterator<K> keyIterator();  
170.     // 返回“逆序”的键迭代器  
171.     abstract Iterator<K> descendingKeyIterator();  
172.  
173.     // 返回SubMap是否为空。空的话,返回true,否则返回false  
174.     public boolean isEmpty() {  
175.         return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();  
176.     }  
177.  
178.     // 返回SubMap的大小  
179.     public int size() {  
180.         return (fromStart && toEnd) ? m.size() : entrySet().size();  
181.     }  
182.  
183.     // 返回SubMap是否包含键key  
184.     public final boolean containsKey(Object key) {  
185.         return inRange(key) && m.containsKey(key);  
186.     }  
187.  
188.     // 将key-value 插入SubMap中  
189.     public final V put(K key, V value) {  
190.         if (!inRange(key))  
191.             throw new IllegalArgumentException("key out of range");  
192.         return m.put(key, value);  
193.     }  
194.  
195.     // 获取key对应值  
196.     public final V get(Object key) {  
197.         return !inRange(key)? null :  m.get(key);  
198.     }  
199.  
200.     // 删除key对应的键值对  
201.     public final V remove(Object key) {  
202.         return !inRange(key)? null  : m.remove(key);  
203.     }  
204.  
205.     // 获取“大于/等于key的最小键值对”  
206.     public final Map.Entry<K,V> ceilingEntry(K key) {  
207.         return exportEntry(subCeiling(key));  
208.     }  
209.  
210.     // 获取“大于/等于key的最小键”  
211.     public final K ceilingKey(K key) {  
212.         return keyOrNull(subCeiling(key));  
213.     }  
214.  
215.     // 获取“大于key的最小键值对”  
216.     public final Map.Entry<K,V> higherEntry(K key) {  
217.         return exportEntry(subHigher(key));  
218.     }  
219.  
220.     // 获取“大于key的最小键”  
221.     public final K higherKey(K key) {  
222.         return keyOrNull(subHigher(key));  
223.     }  
224.  
225.     // 获取“小于/等于key的最大键值对”  
226.     public final Map.Entry<K,V> floorEntry(K key) {  
227.         return exportEntry(subFloor(key));  
228.     }  
229.  
230.     // 获取“小于/等于key的最大键”  
231.     public final K floorKey(K key) {  
232.         return keyOrNull(subFloor(key));  
233.     }  
234.  
235.     // 获取“小于key的最大键值对”  
236.     public final Map.Entry<K,V> lowerEntry(K key) {  
237.         return exportEntry(subLower(key));  
238.     }  
239.  
240.     // 获取“小于key的最大键”  
241.     public final K lowerKey(K key) {  
242.         return keyOrNull(subLower(key));  
243.     }  
244.  
245.     // 获取"SubMap的第一个键"  
246.     public final K firstKey() {  
247.         return key(subLowest());  
248.     }  
249.  
250.     // 获取"SubMap的最后一个键"  
251.     public final K lastKey() {  
252.         return key(subHighest());  
253.     }  
254.  
255.     // 获取"SubMap的第一个键值对"  
256.     public final Map.Entry<K,V> firstEntry() {  
257.         return exportEntry(subLowest());  
258.     }  
259.  
260.     // 获取"SubMap的最后一个键值对"  
261.     public final Map.Entry<K,V> lastEntry() {  
262.         return exportEntry(subHighest());  
263.     }  
264.  
265.     // 返回"SubMap的第一个键值对",并从SubMap中删除改键值对  
266.     public final Map.Entry<K,V> pollFirstEntry() {  
267.     TreeMap.Entry<K,V> e = subLowest();  
268.         Map.Entry<K,V> result = exportEntry(e);  
269.         if (e != null)  
270.             m.deleteEntry(e);  
271.         return result;  
272.     }  
273.  
274.     // 返回"SubMap的最后一个键值对",并从SubMap中删除改键值对  
275.     public final Map.Entry<K,V> pollLastEntry() {  
276.     TreeMap.Entry<K,V> e = subHighest();  
277.         Map.Entry<K,V> result = exportEntry(e);  
278.         if (e != null)  
279.             m.deleteEntry(e);  
280.         return result;  
281.     }  
282.  
283.     // Views  
284.     transient NavigableMap<K,V> descendingMapView = null;  
285.     transient EntrySetView entrySetView = null;  
286.     transient KeySet<K> navigableKeySetView = null;  
287.  
288.     // 返回NavigableSet对象,实际上返回的是当前对象的"Key集合"。   
289.     public final NavigableSet<K> navigableKeySet() {  
290.         KeySet<K> nksv = navigableKeySetView;  
291.         return (nksv != null) ? nksv :  
292.             (navigableKeySetView = new TreeMap.KeySet(this));  
293.     }  
294.  
295.     // 返回"Key集合"对象  
296.     public final Set<K> keySet() {  
297.         return navigableKeySet();  
298.     }  
299.  
300.     // 返回“逆序”的Key集合  
301.     public NavigableSet<K> descendingKeySet() {  
302.         return descendingMap().navigableKeySet();  
303.     }  
304.  
305.     // 排列fromKey(包含) 到 toKey(不包含) 的子map  
306.     public final SortedMap<K,V> subMap(K fromKey, K toKey) {  
307.         return subMap(fromKey, true, toKey, false);  
308.     }  
309.  
310.     // 返回当前Map的头部(从第一个节点 到 toKey, 不包括toKey)  
311.     public final SortedMap<K,V> headMap(K toKey) {  
312.         return headMap(toKey, false);  
313.     }  
314.  
315.     // 返回当前Map的尾部[从 fromKey(包括fromKeyKey) 到 最后一个节点]  
316.     public final SortedMap<K,V> tailMap(K fromKey) {  
317.         return tailMap(fromKey, true);  
318.     }  
319.  
320.     // Map的Entry的集合  
321.     abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {  
322.         private transient int size = -1, sizeModCount;  
323.  
324.         // 获取EntrySet的大小  
325.         public int size() {  
326.             // 若SubMap是从“开始节点”到“结尾节点”,则SubMap大小就是原TreeMap的大小  
327.             if (fromStart && toEnd)  
328.                 return m.size();  
329.             // 若SubMap不是从“开始节点”到“结尾节点”,则调用iterator()遍历EntrySetView中的元素  
330.             if (size == -1 || sizeModCount != m.modCount) {  
331.                 sizeModCount = m.modCount;  
332.                 size = 0;  
333.                 Iterator i = iterator();  
334.                 while (i.hasNext()) {  
335.                     size++;  
336.                     i.next();  
337.                 }  
338.             }  
339.             return size;  
340.         }  
341.  
342.         // 判断EntrySetView是否为空  
343.         public boolean isEmpty() {  
344.             TreeMap.Entry<K,V> n = absLowest();  
345.             return n == null || tooHigh(n.key);  
346.         }  
347.  
348.         // 判断EntrySetView是否包含Object  
349.         public boolean contains(Object o) {  
350.             if (!(o instanceof Map.Entry))  
351.                 return false;  
352.             Map.Entry<K,V> entry = (Map.Entry<K,V>) o;  
353.             K key = entry.getKey();  
354.             if (!inRange(key))  
355.                 return false;  
356.             TreeMap.Entry node = m.getEntry(key);  
357.             return node != null &&  
358.                 valEquals(node.getValue(), entry.getValue());  
359.         }  
360.  
361.         // 从EntrySetView中删除Object  
362.         public boolean remove(Object o) {  
363.             if (!(o instanceof Map.Entry))  
364.                 return false;  
365.             Map.Entry<K,V> entry = (Map.Entry<K,V>) o;  
366.             K key = entry.getKey();  
367.             if (!inRange(key))  
368.                 return false;  
369.             TreeMap.Entry<K,V> node = m.getEntry(key);  
370.             if (node!=null && valEquals(node.getValue(),entry.getValue())){  
371.                 m.deleteEntry(node);  
372.                 return true;  
373.             }  
374.             return false;  
375.         }  
376.     }  
377.  
378.     // SubMap的迭代器  
379.     abstract class SubMapIterator<T> implements Iterator<T> {  
380.         // 上一次被返回的Entry  
381.         TreeMap.Entry<K,V> lastReturned;  
382.         // 指向下一个Entry  
383.         TreeMap.Entry<K,V> next;  
384.         // “栅栏key”。根据SubMap是“升序”还是“降序”具有不同的意义  
385.         final K fenceKey;  
386.         int expectedModCount;  
387.  
388.         // 构造函数  
389.         SubMapIterator(TreeMap.Entry<K,V> first,  
390.                        TreeMap.Entry<K,V> fence) {  
391.             // 每创建一个SubMapIterator时,保存修改次数  
392.             // 若后面发现expectedModCount和modCount不相等,则抛出ConcurrentModificationException异常。  
393.             // 这就是所说的fast-fail机制的原理!  
394.             expectedModCount = m.modCount;  
395.             lastReturned = null;  
396.             next = first;  
397.             fenceKey = fence == null ? null : fence.key;  
398.         }  
399.  
400.         // 是否存在下一个Entry  
401.         public final boolean hasNext() {  
402.             return next != null && next.key != fenceKey;  
403.         }  
404.  
405.         // 返回下一个Entry  
406.         final TreeMap.Entry<K,V> nextEntry() {  
407.             TreeMap.Entry<K,V> e = next;  
408.             if (e == null || e.key == fenceKey)  
409.                 throw new NoSuchElementException();  
410.             if (m.modCount != expectedModCount)  
411.                 throw new ConcurrentModificationException();  
412.             // next指向e的后继节点  
413.             next = successor(e);  
414.     lastReturned = e;  
415.             return e;  
416.         }  
417.  
418.         // 返回上一个Entry  
419.         final TreeMap.Entry<K,V> prevEntry() {  
420.             TreeMap.Entry<K,V> e = next;  
421.             if (e == null || e.key == fenceKey)  
422.                 throw new NoSuchElementException();  
423.             if (m.modCount != expectedModCount)  
424.                 throw new ConcurrentModificationException();  
425.             // next指向e的前继节点  
426.             next = predecessor(e);  
427.     lastReturned = e;  
428.             return e;  
429.         }  
430.  
431.         // 删除当前节点(用于“升序的SubMap”)。  
432.         // 删除之后,可以继续升序遍历;红黑树特性没变。  
433.         final void removeAscending() {  
434.             if (lastReturned == null)  
435.                 throw new IllegalStateException();  
436.             if (m.modCount != expectedModCount)  
437.                 throw new ConcurrentModificationException();  
438.             // 这里重点强调一下“为什么当lastReturned的左右孩子都不为空时,要将其赋值给next”。  
439.             // 目的是为了“删除lastReturned节点之后,next节点指向的仍然是下一个节点”。  
440.             //     根据“红黑树”的特性可知:  
441.             //     当被删除节点有两个儿子时。那么,首先把“它的后继节点的内容”复制给“该节点的内容”;之后,删除“它的后继节点”。  
442.             //     这意味着“当被删除节点有两个儿子时,删除当前节点之后,'新的当前节点'实际上是‘原有的后继节点(即下一个节点)’”。  
443.             //     而此时next仍然指向"新的当前节点"。也就是说next是仍然是指向下一个节点;能继续遍历红黑树。  
444.             if (lastReturned.left != null && lastReturned.right != null)  
445.                 next = lastReturned;  
446.             m.deleteEntry(lastReturned);  
447.             lastReturned = null;  
448.             expectedModCount = m.modCount;  
449.         }  
450.  
451.         // 删除当前节点(用于“降序的SubMap”)。  
452.         // 删除之后,可以继续降序遍历;红黑树特性没变。  
453.         final void removeDescending() {  
454.             if (lastReturned == null)  
455.                 throw new IllegalStateException();  
456.             if (m.modCount != expectedModCount)  
457.                 throw new ConcurrentModificationException();  
458.             m.deleteEntry(lastReturned);  
459.             lastReturned = null;  
460.             expectedModCount = m.modCount;  
461.         }  
462.  
463.     }  
464.  
465.     // SubMap的Entry迭代器,它只支持升序操作,继承于SubMapIterator  
466.     final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {  
467.         SubMapEntryIterator(TreeMap.Entry<K,V> first,  
468.                             TreeMap.Entry<K,V> fence) {  
469.             super(first, fence);  
470.         }  
471.         // 获取下一个节点(升序)  
472.         public Map.Entry<K,V> next() {  
473.             return nextEntry();  
474.         }  
475.         // 删除当前节点(升序)  
476.         public void remove() {  
477.             removeAscending();  
478.         }  
479.     }  
480.  
481.     // SubMap的Key迭代器,它只支持升序操作,继承于SubMapIterator  
482.     final class SubMapKeyIterator extends SubMapIterator<K> {  
483.         SubMapKeyIterator(TreeMap.Entry<K,V> first,  
484.                           TreeMap.Entry<K,V> fence) {  
485.             super(first, fence);  
486.         }  
487.         // 获取下一个节点(升序)  
488.         public K next() {  
489.             return nextEntry().key;  
490.         }  
491.         // 删除当前节点(升序)  
492.         public void remove() {  
493.             removeAscending();  
494.         }  
495.     }  
496.  
497.     // 降序SubMap的Entry迭代器,它只支持降序操作,继承于SubMapIterator  
498.     final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {  
499.         DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last,  
500.                                       TreeMap.Entry<K,V> fence) {  
501.             super(last, fence);  
502.         }  
503.  
504.         // 获取下一个节点(降序)  
505.         public Map.Entry<K,V> next() {  
506.             return prevEntry();  
507.         }  
508.         // 删除当前节点(降序)  
509.         public void remove() {  
510.             removeDescending();  
511.         }  
512.     }  
513.  
514.     // 降序SubMap的Key迭代器,它只支持降序操作,继承于SubMapIterator  
515.     final class DescendingSubMapKeyIterator extends SubMapIterator<K> {  
516.         DescendingSubMapKeyIterator(TreeMap.Entry<K,V> last,  
517.                                     TreeMap.Entry<K,V> fence) {  
518.             super(last, fence);  
519.         }  
520.         // 获取下一个节点(降序)  
521.         public K next() {  
522.             return prevEntry().key;  
523.         }  
524.         // 删除当前节点(降序)  
525.         public void remove() {  
526.             removeDescending();  
527.         }  
528.     }  
529. } 
 
NavigableSubMap源码很多,但不难理解;读者可以通过源码和注释进行理解。
其实,读完NavigableSubMap的源码后,我们可以得出它的核心思想是:它是一个抽象集合类,为2个子类——"(升序)AscendingSubMap"和"(降序)DescendingSubMap"而服务;因为NavigableSubMap实现了许多公共API。它的最终目的是实现下面的一系列函数:
 
1. headMap(K toKey, boolean inclusive)   
2. headMap(K toKey)  
3. subMap(K fromKey, K toKey)  
4. subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)  
5. tailMap(K fromKey)  
6. tailMap(K fromKey, boolean inclusive)  
7. navigableKeySet()   
8. descendingKeySet()