Java荟萃进修(十二) TreeMap具体先容(源码理会)和利用示例
副标题#e#
这一章,我们对TreeMap举办进修。
第1部门 TreeMap先容
TreeMap 简介
TreeMap 是一个有序的key-value荟萃,它是通过红黑树实现的。
TreeMap担任于AbstractMap,所以它是一个Map,即一个key-value荟萃。
TreeMap 实现了NavigableMap接口,意味着它支持一系列的导航要领。好比返回有序的key荟萃。
TreeMap 实现了Cloneable接口,意味着它能被克隆。
TreeMap 实现了java.io.Serializable接口,意味着它支持序列化。
TreeMap基于红黑树(Red-Black tree)实现。该映射按照其键的自然顺序举办排序,可能按照建设映射时提供的 Comparator 举办排序,详细取决于利用的结构要领。
TreeMap的根基操纵 containsKey、get、put 和 remove 的时间巨大度是 log(n) 。
别的,TreeMap长短同步的。 它的iterator 要领返回的迭代器是fail-fastl的。
TreeMap的担任干系
java.lang.Object
java.util.AbstractMap<K, V>
java.util.TreeMap<K, V>
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable {}
TreeMap与Map干系如下图:

TreeMap的结构函数
// 默认结构函数。利用该结构函数,TreeMap中的元素凭据自然排序举办分列。
TreeMap()
// 建设的TreeMap包括Map
TreeMap(Map<? extends K, ? extends V> copyFrom)
// 指定Tree的较量器
TreeMap(Comparator<? super K> comparator)
// 建设的TreeSet包括copyFrom
TreeMap(SortedMap<K, ? extends V> copyFrom)
TreeMap的API
Entry<K, V> ceilingEntry(K key) K ceilingKey(K key) void clear() Object clone() Comparator<? super K> comparator() boolean containsKey(Object key) NavigableSet<K> descendingKeySet() NavigableMap<K, V> descendingMap() Set<Entry<K, V>> entrySet() Entry<K, V> firstEntry() K firstKey() Entry<K, V> floorEntry(K key) K floorKey(K key) V get(Object key) NavigableMap<K, V> headMap(K to, boolean inclusive) SortedMap<K, V> headMap(K toExclusive) Entry<K, V> higherEntry(K key) K higherKey(K key) boolean isEmpty() Set<K> keySet() Entry<K, V> lastEntry() K lastKey() Entry<K, V> lowerEntry(K key) K lowerKey(K key) NavigableSet<K> navigableKeySet() Entry<K, V> pollFirstEntry() Entry<K, V> pollLastEntry() V put(K key, V value) V remove(Object key) int size() SortedMap<K, V> subMap(K fromInclusive, K toExclusive) NavigableMap<K, V> subMap(K from, boolean fromInclusive, K to, boolean toInclusive) NavigableMap<K, V> tailMap(K from, boolean inclusive) SortedMap<K, V> tailMap(K fromInclusive)
第2部门 TreeMap源码理会
为了更相识TreeMap的道理,下面临TreeMap源码代码作出阐明。我们先给出源码内容,后头再对源码举办具体说明,虽然,源码内容中也包括了具体的代码注释。读者阅读的时候,发起先看后头的说明,先成立一个整体印象;之后再阅读源码。
package java.util;
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
{
// 较量器。用来给TreeMap排序
private final Comparator<? super K> comparator;
// TreeMap是红黑树实现的,root是红黑书的根节点
private transient Entry<K,V> root = null;
// 红黑树的节点总数
private transient int size = 0;
// 记录红黑树的修改次数
private transient int modCount = 0;
// 默认结构函数
public TreeMap() {
comparator = null;
}
// 带较量器的结构函数
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
// 带Map的结构函数,Map会成为TreeMap的子集
public TreeMap(Map<? extends K, ? extends V> m) {
comparator = null;
putAll(m);
}
// 带SortedMap的结构函数,SortedMap会成为TreeMap的子集
public TreeMap(SortedMap<K, ? extends V> m) {
comparator = m.comparator();
try {
buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
} catch (java.io.IOException cannotHappen) {
} catch (ClassNotFoundException cannotHappen) {
}
}
public int size() {
return size;
}
// 返回TreeMap中是否掩护“键(key)”
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
// 返回TreeMap中是否掩护"值(value)"
public boolean containsValue(Object value) {
// getFirstEntry() 是返回红黑树的第一个节点
// successor(e) 是获取节点e的后继节点
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e))
if (valEquals(value, e.value))
return true;
return false;
}
// 获取“键(key)”对应的“值(value)”
public V get(Object key) {
// 获取“键”为key的节点(p)
Entry<K,V> p = getEntry(key);
// 若节点(p)为null,返回null;不然,返回节点对应的值
return (p==null ? null : p.value);
}
public Comparator<? super K> comparator() {
return comparator;
}
// 获取第一个节点对应的key
public K firstKey() {
return key(getFirstEntry());
}
// 获取最后一个节点对应的key
public K lastKey() {
return key(getLastEntry());
}
// 将map中的全部节点添加到TreeMap中
public void putAll(Map<? extends K, ? extends V> map) {
// 获取map的巨细
int mapSize = map.size();
// 假如TreeMap的巨细是0,且map的巨细不是0,且map是已排序的“key-value对”
if (size==0 && mapSize!=0 && map instanceof SortedMap) {
Comparator c = ((SortedMap)map).comparator();
// 假如TreeMap和map的较量器相等;
// 则将map的元素全部拷贝到TreeMap中,然后返回!
if (c == comparator || (c != null && c.equals(comparator))) {
++modCount;
try {
buildFromSorted(mapSize, map.entrySet().iterator(),
null, null);
} catch (java.io.IOException cannotHappen) {
} catch (ClassNotFoundException cannotHappen) {
}
return;
}
}
// 挪用AbstractMap中的putAll();
// AbstractMap中的putAll()又会挪用到TreeMap的put()
super.putAll(map);
}
// 获取TreeMap中“键”为key的节点
final Entry<K,V> getEntry(Object key) {
// 若“较量器”为null,则通过getEntryUsingComparator()获取“键”为key的节点
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
// 将p设为根节点
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
// 若“p的key” < key,则p=“p的左孩子”
if (cmp < 0)
p = p.left;
// 若“p的key” > key,则p=“p的左孩子”
else if (cmp > 0)
p = p.right;
// 若“p的key” = key,则返回节点p
else
return p;
}
return null;
}
// 获取TreeMap中“键”为key的节点(对应TreeMap的较量器不是null的环境)
final Entry<K,V> getEntryUsingComparator(Object key) {
K k = (K) key;
Comparator<? super K> cpr = comparator;
if (cpr != null) {
// 将p设为根节点
Entry<K,V> p = root;
while (p != null) {
int cmp = cpr.compare(k, p.key);
// 若“p的key” < key,则p=“p的左孩子”
if (cmp < 0)
p = p.left;
// 若“p的key” > key,则p=“p的左孩子”
else if (cmp > 0)
p = p.right;
// 若“p的key” = key,则返回节点p
else
return p;
}
}
return null;
}
// 获取TreeMap中不小于key的最小的节点;
// 若不存在(即TreeMap中所有节点的键都比key大),就返回null
final Entry<K,V> getCeilingEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
// 环境一:若“p的key” > key。
// 若 p 存在左孩子,则设 p=“p的左孩子”;
// 不然,返回p
if (cmp < 0) {
if (p.left != null)
p = p.left;
else
return p;
// 环境二:若“p的key” < key。
} else if (cmp > 0) {
// 若 p 存在右孩子,则设 p=“p的右孩子”
if (p.right != null) {
p = p.right;
} else {
// 若 p 不存在右孩子,则找出 p 的后继节点,并返回
// 留意:这里返回的 “p的后继节点”有2种大概性:第一,null;第二,TreeMap中大于key的最小的节点。
// 领略这一点的焦点是,getCeilingEntry是从root开始遍历的。
// 若getCeilingEntry能走到这一步,那么,它之前“已经遍历过的节点的key”都 > key。
// 能领略上面所说的,那么就很容易大白,为什么“p的后继节点”又2种大概性了。
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
}
return parent;
}
// 环境三:若“p的key” = key。
} else
return p;
}
return null;
}
// 获取TreeMap中不大于key的最大的节点;
// 若不存在(即TreeMap中所有节点的键都比key小),就返回null
// getFloorEntry的道理和getCeilingEntry雷同,这里不再多说。
final Entry<K,V> getFloorEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp > 0) {
if (p.right != null)
p = p.right;
else
return p;
} else if (cmp < 0) {
if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
}
return parent;
}
} else
return p;
}
return null;
}
// 获取TreeMap中大于key的最小的节点。
// 若不存在,就返回null。
// 请参照getCeilingEntry来对getHigherEntry举办领略。
final Entry<K,V> getHigherEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp < 0) {
if (p.left != null)
p = p.left;
else
return p;
} else {
if (p.right != null) {
p = p.right;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
}
return parent;
}
}
}
return null;
}
// 获取TreeMap中小于key的最大的节点。
// 若不存在,就返回null。
// 请参照getCeilingEntry来对getLowerEntry举办领略。
final Entry<K,V> getLowerEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp > 0) {
if (p.right != null)
p = p.right;
else
return p;
} else {
if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
}
return parent;
}
}
}
return null;
}
// 将“key, value”添加到TreeMap中
// 领略TreeMap的前提是把握“红黑树”。
// 若领略“红黑树中添加节点”的算法,则很容易领略put。
public V put(K key, V value) {
Entry<K,V> t = root;
// 若红黑树为空,则插入根节点
if (t == null) {
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry<K,V>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
// 在二叉树(红黑树是非凡的二叉树)中,找到(key, value)的插入位置。
// 红黑树是以key来举办排序的,所以这里以key来举办查找。
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
// 新建红黑树的节点(e)
Entry<K,V> e = new Entry<K,V>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
// 红黑树插入节点后,不再是一颗红黑树;
// 这里通过fixAfterInsertion的处理惩罚,来规复红黑树的特性。
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
// 删除TreeMap中的键为key的节点,并返回节点的值
public V remove(Object key) {
// 找到键为key的节点
Entry<K,V> p = getEntry(key);
if (p == null)
return null;
// 生存节点的值
V oldValue = p.value;
// 删除节点
deleteEntry(p);
return oldValue;
}
// 清空红黑树
public void clear() {
modCount++;
size = 0;
root = null;
}
// 克隆一个TreeMap,并返回Object工具
public Object clone() {
TreeMap<K,V> clone = null;
try {
clone = (TreeMap<K,V>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
// Put clone into "virgin" state (except for comparator)
clone.root = null;
clone.size = 0;
clone.modCount = 0;
clone.entrySet = null;
clone.navigableKeySet = null;
clone.descendingMap = null;
// Initialize clone with our mappings
try {
clone.buildFromSorted(size, entrySet().iterator(), null, null);
} catch (java.io.IOException cannotHappen) {
} catch (ClassNotFoundException cannotHappen) {
}
return clone;
}
// 获取第一个节点(对外接口)。
public Map.Entry<K,V> firstEntry() {
return exportEntry(getFirstEntry());
}
// 获取最后一个节点(对外接口)。
public Map.Entry<K,V> lastEntry() {
return exportEntry(getLastEntry());
}
// 获取第一个节点,并将改节点从TreeMap中删除。
public Map.Entry<K,V> pollFirstEntry() {
// 获取第一个节点
Entry<K,V> p = getFirstEntry();
Map.Entry<K,V> result = exportEntry(p);
// 删除第一个节点
if (p != null)
deleteEntry(p);
return result;
}
// 获取最后一个节点,并将改节点从TreeMap中删除。
public Map.Entry<K,V> pollLastEntry() {
// 获取最后一个节点
Entry<K,V> p = getLastEntry();
Map.Entry<K,V> result = exportEntry(p);
// 删除最后一个节点
if (p != null)
deleteEntry(p);
return result;
}
// 返回小于key的最大的键值对,没有的话返回null
public Map.Entry<K,V> lowerEntry(K key) {
return exportEntry(getLowerEntry(key));
}
// 返回小于key的最大的键值对所对应的KEY,没有的话返回null
public K lowerKey(K key) {
return keyOrNull(getLowerEntry(key));
}
// 返回不大于key的最大的键值对,没有的话返回null
public Map.Entry<K,V> floorEntry(K key) {
return exportEntry(getFloorEntry(key));
}
// 返回不大于key的最大的键值对所对应的KEY,没有的话返回null
public K floorKey(K key) {
return keyOrNull(getFloorEntry(key));
}
// 返回不小于key的最小的键值对,没有的话返回null
public Map.Entry<K,V> ceilingEntry(K key) {
return exportEntry(getCeilingEntry(key));
}
// 返回不小于key的最小的键值对所对应的KEY,没有的话返回null
public K ceilingKey(K key) {
return keyOrNull(getCeilingEntry(key));
}
// 返回大于key的最小的键值对,没有的话返回null
public Map.Entry<K,V> higherEntry(K key) {
return exportEntry(getHigherEntry(key));
}
// 返回大于key的最小的键值对所对应的KEY,没有的话返回null
public K higherKey(K key) {
return keyOrNull(getHigherEntry(key));
}
// TreeMap的红黑树节点对应的荟萃
private transient EntrySet entrySet = null;
// KeySet为KeySet导航类
private transient KeySet<K> navigableKeySet = null;
// descendingMap为键值对的倒序“映射”
private transient NavigableMap<K,V> descendingMap = null;
// 返回TreeMap的“键的荟萃”
public Set<K> keySet() {
return navigableKeySet();
}
// 获取“可导航”的Key的荟萃
// 实际上是返回KeySet类的工具。
public NavigableSet<K> navigableKeySet() {
KeySet<K> nks = navigableKeySet;
return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
}
// 返回“TreeMap的值对应的荟萃”
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null) ? vs : (values = new Values());
}
// 获取TreeMap的Entry的荟萃,实际上是返回EntrySet类的工具。
public Set<Map.Entry<K,V>> entrySet() {
EntrySet es = entrySet;
return (es != null) ? es : (entrySet = new EntrySet());
}
// 获取TreeMap的降序Map
// 实际上是返回DescendingSubMap类的工具
public NavigableMap<K, V> descendingMap() {
NavigableMap<K, V> km = descendingMap;
return (km != null) ? km :
(descendingMap = new DescendingSubMap(this,
true, null, true,
true, null, true));
}
// 获取TreeMap的子Map
// 范畴是从fromKey 到 toKey;fromInclusive是是否包括fromKey的标志,toInclusive是是否包括toKey的标志
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) {
return new AscendingSubMap(this,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
// 获取“Map的头部”
// 范畴从第一个节点 到 toKey, inclusive是是否包括toKey的标志
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
return new AscendingSubMap(this,
true, null, true,
false, toKey, inclusive);
}
// 获取“Map的尾部”。
// 范畴是从 fromKey 到 最后一个节点,inclusive是是否包括fromKey的标志
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
return new AscendingSubMap(this,
false, fromKey, inclusive,
true, null, true);
}
// 获取“子Map”。
// 范畴是从fromKey(包罗) 到 toKey(不包罗)
public SortedMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}
// 获取“Map的头部”。
// 范畴从第一个节点 到 toKey(不包罗)
public SortedMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}
// 获取“Map的尾部”。
// 范畴是从 fromKey(包罗) 到 最后一个节点
public SortedMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}
// ”TreeMap的值的荟萃“对应的类,它集成于AbstractCollection
class Values extends AbstractCollection<V> {
// 返回迭代器
public Iterator<V> iterator() {
return new ValueIterator(getFirstEntry());
}
// 返回个数
public int size() {
return TreeMap.this.size();
}
// "TreeMap的值的荟萃"中是否包括"工具o"
public boolean contains(Object o) {
return TreeMap.this.containsValue(o);
}
// 删除"TreeMap的值的荟萃"中的"工具o"
public boolean remove(Object o) {
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
if (valEquals(e.getValue(), o)) {
deleteEntry(e);
return true;
}
}
return false;
}
// 清空删除"TreeMap的值的荟萃"
public void clear() {
TreeMap.this.clear();
}
}
// EntrySet是“TreeMap的所有键值对构成的荟萃”,
// EntrySet荟萃的单元是单个“键值对”。
class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator(getFirstEntry());
}
// EntrySet中是否包括“键值对Object”
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
V value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}
// 删除EntrySet中的“键值对Object”
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
V value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
if (p != null && valEquals(p.getValue(), value)) {
deleteEntry(p);
return true;
}
return false;
}
// 返回EntrySet中元素个数
public int size() {
return TreeMap.this.size();
}
// 清空EntrySet
public void clear() {
TreeMap.this.clear();
}
}
// 返回“TreeMap的KEY构成的迭代器(顺序)”
Iterator<K> keyIterator() {
return new KeyIterator(getFirstEntry());
}
// 返回“TreeMap的KEY构成的迭代器(逆序)”
Iterator<K> descendingKeyIterator() {
return new DescendingKeyIterator(getLastEntry());
}
// KeySet是“TreeMap中所有的KEY构成的荟萃”
// KeySet担任于AbstractSet,并且实现了NavigableSet接口。
static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
// NavigableMap成员,KeySet是通过NavigableMap实现的
private final NavigableMap<E, Object> m;
KeySet(NavigableMap<E,Object> map) { m = map; }
// 升序迭代器
public Iterator<E> iterator() {
// 若是TreeMap工具,则挪用TreeMap的迭代器keyIterator()
// 不然,挪用TreeMap子类NavigableSubMap的迭代器keyIterator()
if (m instanceof TreeMap)
return ((TreeMap<E,Object>)m).keyIterator();
else
return (Iterator<E>)(((TreeMap.NavigableSubMap)m).keyIterator());
}
// 降序迭代器
public Iterator<E> descendingIterator() {
// 若是TreeMap工具,则挪用TreeMap的迭代器descendingKeyIterator()
// 不然,挪用TreeMap子类NavigableSubMap的迭代器descendingKeyIterator()
if (m instanceof TreeMap)
return ((TreeMap<E,Object>)m).descendingKeyIterator();
else
return (Iterator<E>)(((TreeMap.NavigableSubMap)m).descendingKeyIterator());
}
public int size() { return m.size(); }
public boolean isEmpty() { return m.isEmpty(); }
public boolean contains(Object o) { return m.containsKey(o); }
public void clear() { m.clear(); }
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); }
public E first() { return m.firstKey(); }
public E last() { return m.lastKey(); }
public Comparator<? super E> comparator() { return m.comparator(); }
public E pollFirst() {
Map.Entry<E,Object> e = m.pollFirstEntry();
return e == null? null : e.getKey();
}
public E pollLast() {
Map.Entry<E,Object> e = m.pollLastEntry();
return e == null? null : e.getKey();
}
public boolean remove(Object o) {
int oldSize = size();
m.remove(o);
return size() != oldSize;
}
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
E toElement, boolean toInclusive) {
return new TreeSet<E>(m.subMap(fromElement, fromInclusive,
toElement, toInclusive));
}
public NavigableSet<E> headSet(E toElement, boolean inclusive) {
return new TreeSet<E>(m.headMap(toElement, inclusive));
}
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new TreeSet<E>(m.tailMap(fromElement, inclusive));
}
public SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
public SortedSet<E> headSet(E toElement) {
return headSet(toElement, false);
}
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
public NavigableSet<E> descendingSet() {
return new TreeSet(m.descendingMap());
}
}
// 它是TreeMap中的一个抽象迭代器,实现了一些通用的接口。
abstract class PrivateEntryIterator<T> implements Iterator<T> {
// 下一个元素
Entry<K,V> next;
// 上一次返回元素
Entry<K,V> lastReturned;
// 期望的修改次数,用于实现fast-fail机制
int expectedModCount;
PrivateEntryIterator(Entry<K,V> first) {
expectedModCount = modCount;
lastReturned = null;
next = first;
}
public final boolean hasNext() {
return next != null;
}
// 获取下一个节点
final Entry<K,V> nextEntry() {
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
next = successor(e);
lastReturned = e;
return e;
}
// 获取上一个节点
final Entry<K,V> prevEntry() {
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
next = predecessor(e);
lastReturned = e;
return e;
}
// 删除当前节点
public void remove() {
if (lastReturned == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
// 这里重点强调一下“为什么当lastReturned的阁下孩子都不为空时,要将其赋值给next”。
// 目标是为了“删除lastReturned节点之后,next节点指向的仍然是下一个节点”。
// 按照“红黑树”的特性可知:
// 当被删除节点有两个儿子时。那么,首先把“它的后继节点的内容”复制给“该节点的内容”;之后,删除“它的后继节点”。
// 这意味着“当被删除节点有两个儿子时,删除当前节点之后,'新的当前节点'实际上是‘原有的后继节点(即下一个节点)’”。
// 而此时next仍然指向"新的当前节点"。也就是说next是仍然是指向下一个节点;能继承遍历红黑树。
if (lastReturned.left != null && lastReturned.right != null)
next = lastReturned;
deleteEntry(lastReturned);
expectedModCount = modCount;
lastReturned = null;
}
}
// TreeMap的Entry对应的迭代器
final class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
EntryIterator(Entry<K,V> first) {
super(first);
}
public Map.Entry<K,V> next() {
return nextEntry();
}
}
// TreeMap的Value对应的迭代器
final class ValueIterator extends PrivateEntryIterator<V> {
ValueIterator(Entry<K,V> first) {
super(first);
}
public V next() {
return nextEntry().value;
}
}
// reeMap的KEY构成的迭代器(顺序)
final class KeyIterator extends PrivateEntryIterator<K> {
KeyIterator(Entry<K,V> first) {
super(first);
}
public K next() {
return nextEntry().key;
}
}
// TreeMap的KEY构成的迭代器(逆序)
final class DescendingKeyIterator extends PrivateEntryIterator<K> {
DescendingKeyIterator(Entry<K,V> first) {
super(first);
}
public K next() {
return prevEntry().key;
}
}
// 较量两个工具的巨细
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
// 判定两个工具是否相等
final static boolean valEquals(Object o1, Object o2) {
return (o1==null ? o2==null : o1.equals(o2));
}
// 返回“Key-Value键值对”的一个简朴拷贝(AbstractMap.SimpleImmutableEntry<K,V>工具)
// 可用来读取“键值对”的值
static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
return e == null? null :
new AbstractMap.SimpleImmutableEntry<K,V>(e);
}
// 若“键值对”不为null,则返回KEY;不然,返回null
static <K,V> K keyOrNull(TreeMap.Entry<K,V> e) {
return e == null? null : e.key;
}
// 若“键值对”不为null,则返回KEY;不然,抛出异常
static <K> K key(Entry<K,?> e) {
if (e==null)
throw new NoSuchElementException();
return e.key;
}
// TreeMap的SubMap,它一个抽象类,实现了民众操纵。
// 它包罗了"(升序)AscendingSubMap"和"(降序)DescendingSubMap"两个子类。
static abstract class NavigableSubMap<K,V> extends AbstractMap<K,V>
implements NavigableMap<K,V>, java.io.Serializable {
// TreeMap的拷贝
final TreeMap<K,V> m;
// lo是“子Map范畴的最小值”,hi是“子Map范畴的最大值”;
// loInclusive是“是否包括lo的标志”,hiInclusive是“是否包括hi的标志”
// fromStart是“暗示是否从第一个节点开始计较”,
// toEnd是“暗示是否计较到最后一个节点 ”
final K lo, hi;
final boolean fromStart, toEnd;
final boolean loInclusive, hiInclusive;
// 结构函数
NavigableSubMap(TreeMap<K,V> m,
boolean fromStart, K lo, boolean loInclusive,
boolean toEnd, K hi, boolean hiInclusive) {
if (!fromStart && !toEnd) {
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;
}
// 判定key是否太小
final boolean tooLow(Object key) {
// 若该SubMap不包罗“起始节点”,
// 而且,“key小于最小键(lo)”可能“key便是最小键(lo),但最小键却没包罗在该SubMap内”
// 则判定key太小。其余环境都不是太小!
if (!fromStart) {
int c = m.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive))
return true;
}
return false;
}
// 判定key是否太大
final boolean tooHigh(Object key) {
// 若该SubMap不包罗“竣事节点”,
// 而且,“key大于最大键(hi)”可能“key便是最大键(hi),但最大键却没包罗在该SubMap内”
// 则判定key太大。其余环境都不是太大!
if (!toEnd) {
int c = m.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive))
return true;
}
return false;
}
// 判定key是否在“lo和hi”开区间范畴内
final boolean inRange(Object key) {
return !tooLow(key) && !tooHigh(key);
}
// 判定key是否在关闭区间内
final boolean inClosedRange(Object key) {
return (fromStart || m.compare(key, lo) >= 0)
&& (toEnd || m.compare(hi, key) >= 0);
}
// 判定key是否在区间内, inclusive是区间开关符号
final boolean inRange(Object key, boolean inclusive) {
return inclusive ? inRange(key) : inClosedRange(key);
}
// 返回最低的Entry
final TreeMap.Entry<K,V> absLowest() {
// 若“包括起始节点”,则挪用getFirstEntry()返回第一个节点
// 不然的话,若包罗lo,则挪用getCeilingEntry(lo)获取大于/便是lo的最小的Entry;
// 不然,挪用getHigherEntry(lo)获取大于lo的最小Entry
TreeMap.Entry<K,V> e =
(fromStart ? m.getFirstEntry() :
(loInclusive ? m.getCeilingEntry(lo) :
m.getHigherEntry(lo)));
return (e == null || tooHigh(e.key)) ? null : e;
}
// 返回最高的Entry
final TreeMap.Entry<K,V> absHighest() {
// 若“包括竣事节点”,则挪用getLastEntry()返回最后一个节点
// 不然的话,若包罗hi,则挪用getFloorEntry(hi)获取小于/便是hi的最大的Entry;
// 不然,挪用getLowerEntry(hi)获取大于hi的最大Entry
TreeMap.Entry<K,V> e =
TreeMap.Entry<K,V> e =
(toEnd ? m.getLastEntry() :
(hiInclusive ? m.getFloorEntry(hi) :
m.getLowerEntry(hi)));
return (e == null || tooLow(e.key)) ? null : e;
}
// 返回"大于/便是key的最小的Entry"
final TreeMap.Entry<K,V> absCeiling(K key) {
// 只有在“key太小”的环境下,absLowest()返回的Entry才是“大于/便是key的最小Entry”
// 其它环境下不可。譬喻,当包括“起始节点”时,absLowest()返回的是最小Entry了!
if (tooLow(key))
return absLowest();
// 获取“大于/便是key的最小Entry”
TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
// 返回"大于key的最小的Entry"
final TreeMap.Entry<K,V> absHigher(K key) {
// 只有在“key太小”的环境下,absLowest()返回的Entry才是“大于key的最小Entry”
// 其它环境下不可。譬喻,当包括“起始节点”时,absLowest()返回的是最小Entry了,而不必然是“大于key的最小Entry”!
if (tooLow(key))
return absLowest();
// 获取“大于key的最小Entry”
TreeMap.Entry<K,V> e = m.getHigherEntry(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
// 返回"小于/便是key的最大的Entry"
final TreeMap.Entry<K,V> absFloor(K key) {
// 只有在“key太大”的环境下,(absHighest)返回的Entry才是“小于/便是key的最大Entry”
// 其它环境下不可。譬喻,当包括“竣事节点”时,absHighest()返回的是最大Entry了!
if (tooHigh(key))
return absHighest();
// 获取"小于/便是key的最大的Entry"
TreeMap.Entry<K,V> e = m.getFloorEntry(key);
return (e == null || tooLow(e.key)) ? null : e;
}
// 返回"小于key的最大的Entry"
final TreeMap.Entry<K,V> absLower(K key) {
// 只有在“key太大”的环境下,(absHighest)返回的Entry才是“小于key的最大Entry”
// 其它环境下不可。譬喻,当包括“竣事节点”时,absHighest()返回的是最大Entry了,而不必然是“小于key的最大Entry”!
if (tooHigh(key))
return absHighest();
// 获取"小于key的最大的Entry"
TreeMap.Entry<K,V> e = m.getLowerEntry(key);
return (e == null || tooLow(e.key)) ? null : e;
}
// 返回“大于最大节点中的最小节点”,不存在的话,返回null
final TreeMap.Entry<K,V> absHighFence() {
return (toEnd ? null : (hiInclusive ?
m.getHigherEntry(hi) :
m.getCeilingEntry(hi)));
}
// 返回“小于最小节点中的最大节点”,不存在的话,返回null
final TreeMap.Entry<K,V> absLowFence() {
return (fromStart ? null : (loInclusive ?
m.getLowerEntry(lo) :
m.getFloorEntry(lo)));
}
// 下面几个abstract要领是需要NavigableSubMap的实现类实现的要领
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();
// 返回SubMap是否为空。空的话,返回true,不然返回false
public boolean isEmpty() {
return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();
}
// 返回SubMap的巨细
public int size() {
return (fromStart && toEnd) ? m.size() : entrySet().size();
}
// 返回SubMap是否包括键key
public final boolean containsKey(Object key) {
return inRange(key) && m.containsKey(key);
}
// 将key-value 插入SubMap中
public final V put(K key, V value) {
if (!inRange(key))
throw new IllegalArgumentException("key out of range");
return m.put(key, value);
}
// 获取key对应值
public final V get(Object key) {
return !inRange(key)? null : m.get(key);
}
// 删除key对应的键值对
public final V remove(Object key) {
return !inRange(key)? null : m.remove(key);
}
// 获取“大于/便是key的最小键值对”
public final Map.Entry<K,V> ceilingEntry(K key) {
return exportEntry(subCeiling(key));
}
// 获取“大于/便是key的最小键”
public final K ceilingKey(K key) {
return keyOrNull(subCeiling(key));
}
// 获取“大于key的最小键值对”
public final Map.Entry<K,V> higherEntry(K key) {
return exportEntry(subHigher(key));
}
// 获取“大于key的最小键”
public final K higherKey(K key) {
return keyOrNull(subHigher(key));
}
// 获取“小于/便是key的最大键值对”
public final Map.Entry<K,V> floorEntry(K key) {
return exportEntry(subFloor(key));
}
// 获取“小于/便是key的最大键”
public final K floorKey(K key) {
return keyOrNull(subFloor(key));
}
// 获取“小于key的最大键值对”
public final Map.Entry<K,V> lowerEntry(K key) {
return exportEntry(subLower(key));
}
// 获取“小于key的最大键”
public final K lowerKey(K key) {
return keyOrNull(subLower(key));
}
// 获取"SubMap的第一个键"
public final K firstKey() {
return key(subLowest());
}
// 获取"SubMap的最后一个键"
public final K lastKey() {
return key(subHighest());
}
// 获取"SubMap的第一个键值对"
public final Map.Entry<K,V> firstEntry() {
return exportEntry(subLowest());
}
// 获取"SubMap的最后一个键值对"
public final Map.Entry<K,V> lastEntry() {
return exportEntry(subHighest());
}
// 返回"SubMap的第一个键值对",并从SubMap中删除改键值对
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;
}
// 返回"SubMap的最后一个键值对",并从SubMap中删除改键值对
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;
}
// Views
transient NavigableMap<K,V> descendingMapView = null;
transient EntrySetView entrySetView = null;
transient KeySet<K> navigableKeySetView = null;
// 返回NavigableSet工具,实际上返回的是当前工具的"Key荟萃"。
public final NavigableSet<K> navigableKeySet() {
KeySet<K> nksv = navigableKeySetView;
return (nksv != null) ? nksv :
(navigableKeySetView = new TreeMap.KeySet(this));
}
// 返回"Key荟萃"工具
public final Set<K> keySet() {
return navigableKeySet();
}
// 返回“逆序”的Key荟萃
public NavigableSet<K> descendingKeySet() {
return descendingMap().navigableKeySet();
}
// 分列fromKey(包括) 到 toKey(不包括) 的子map
public final SortedMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}
// 返回当前Map的头部(从第一个节点 到 toKey, 不包罗toKey)
public final SortedMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}
// 返回当前Map的尾部[从 fromKey(包罗fromKeyKey) 到 最后一个节点]
public final SortedMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}
// Map的Entry的荟萃
abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
private transient int size = -1, sizeModCount;
// 获取EntrySet的巨细
public int size() {
// 若SubMap是从“开始节点”到“末了节点”,则SubMap巨细就是原TreeMap的巨细
if (fromStart && toEnd)
return m.size();
// 若SubMap不是从“开始节点”到“末了节点”,则挪用iterator()遍历EntrySetView中的元素
if (size == -1 || sizeModCount != m.modCount) {
sizeModCount = m.modCount;
size = 0;
Iterator i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
}
return size;
}
// 判定EntrySetView是否为空
public boolean isEmpty() {
TreeMap.Entry<K,V> n = absLowest();
return n == null || tooHigh(n.key);
}
// 判定EntrySetView是否包括Object
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();
if (!inRange(key))
return false;
TreeMap.Entry node = m.getEntry(key);
return node != null &&
valEquals(node.getValue(), entry.getValue());
}
// 从EntrySetView中删除Object
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);
if (node!=null && valEquals(node.getValue(),entry.getValue())){
m.deleteEntry(node);
return true;
}
return false;
}
}
// SubMap的迭代器
abstract class SubMapIterator<T> implements Iterator<T> {
// 上一次被返回的Entry
TreeMap.Entry<K,V> lastReturned;
// 指向下一个Entry
TreeMap.Entry<K,V> next;
// “栅栏key”。按照SubMap是“升序”照旧“降序”具有差异的意义
final K fenceKey;
int expectedModCount;
// 结构函数
SubMapIterator(TreeMap.Entry<K,V> first,
TreeMap.Entry<K,V> fence) {
// 每建设一个SubMapIterator时,生存修改次数
// 若后头发明expectedModCount和modCount不相等,则抛出ConcurrentModificationException异常。
// 这就是所说的fast-fail机制的道理!
expectedModCount = m.modCount;
lastReturned = null;
next = first;
fenceKey = fence == null ? null : fence.key;
}
// 是否存在下一个Entry
public final boolean hasNext() {
return next != null && next.key != fenceKey;
}
// 返回下一个Entry
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指向e的后继节点
next = successor(e);
lastReturned = e;
return e;
}
// 返回上一个Entry
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指向e的前继节点
next = predecessor(e);
lastReturned = e;
return e;
}
// 删除当前节点(用于“升序的SubMap”)。
// 删除之后,可以继承升序遍历;红黑树特性没变。
final void removeAscending() {
if (lastReturned == null)
throw new IllegalStateException();
if (m.modCount != expectedModCount)
throw new ConcurrentModificationException();
// 这里重点强调一下“为什么当lastReturned的阁下孩子都不为空时,要将其赋值给next”。
// 目标是为了“删除lastReturned节点之后,next节点指向的仍然是下一个节点”。
// 按照“红黑树”的特性可知:
// 当被删除节点有两个儿子时。那么,首先把“它的后继节点的内容”复制给“该节点的内容”;之后,删除“它的后继节点”。
// 这意味着“当被删除节点有两个儿子时,删除当前节点之后,'新的当前节点'实际上是‘原有的后继节点(即下一个节点)’”。
// 而此时next仍然指向"新的当前节点"。也就是说next是仍然是指向下一个节点;能继承遍历红黑树。
if (lastReturned.left != null && lastReturned.right != null)
next = lastReturned;
m.deleteEntry(lastReturned);
lastReturned = null;
expectedModCount = m.modCount;
}
// 删除当前节点(用于“降序的SubMap”)。
// 删除之后,可以继承降序遍历;红黑树特性没变。
final void removeDescending() {
if (lastReturned == null)
throw new IllegalStateException();
if (m.modCount != expectedModCount)
throw new ConcurrentModificationException();
m.deleteEntry(lastReturned);
lastReturned = null;
expectedModCount = m.modCount;
}
}
// SubMap的Entry迭代器,它只支持升序操纵,担任于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();
}
}
// SubMap的Key迭代器,它只支持升序操纵,担任于SubMapIterator
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();
}
}
// 降序SubMap的Entry迭代器,它只支持降序操纵,担任于SubMapIterator
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();
}
}
// 降序SubMap的Key迭代器,它只支持降序操纵,担任于SubMapIterator
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();
}
}
}
// 升序的SubMap,担任于NavigableSubMap
static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V> {
private static final long serialVersionUID = 912986545866124060L;
// 结构函数
AscendingSubMap(TreeMap<K,V> m,
boolean fromStart, K lo, boolean loInclusive,
boolean toEnd, K hi, boolean hiInclusive) {
super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
// 较量器
public Comparator<? super K> comparator() {
return m.comparator();
}
// 获取“子Map”。
// 范畴是从fromKey 到 toKey;fromInclusive是是否包括fromKey的标志,toInclusive是是否包括toKey的标志
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
return new AscendingSubMap(m,
false, fromKey, fromInclusive,
false, toKey, toInclusive);
}
// 获取“Map的头部”。
// 范畴从第一个节点 到 toKey, inclusive是是否包括toKey的标志
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
if (!inRange(toKey, inclusive))
throw new IllegalArgumentException("toKey out of range");
return new AscendingSubMap(m,
fromStart, lo, loInclusive,
false, toKey, inclusive);
}
// 获取“Map的尾部”。
// 范畴是从 fromKey 到 最后一个节点,inclusive是是否包括fromKey的标志
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive){
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
return new AscendingSubMap(m,
false, fromKey, inclusive,
toEnd, hi, hiInclusive);
}
// 获取对应的降序Map
public NavigableMap<K,V> descendingMap() {
NavigableMap<K,V> mv = descendingMapView;
return (mv != null) ? mv :
(descendingMapView =
new DescendingSubMap(m,
fromStart, lo, loInclusive,
toEnd, hi, hiInclusive));
}
// 返回“升序Key迭代器”
Iterator<K> keyIterator() {
return new SubMapKeyIterator(absLowest(), absHighFence());
}
// 返回“降序Key迭代器”
Iterator<K> descendingKeyIterator() {
return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
}
// “升序EntrySet荟萃”类
// 实现了iterator()
final class AscendingEntrySetView extends EntrySetView {
public Iterator<Map.Entry<K,V>> iterator() {
return new SubMapEntryIterator(absLowest(), absHighFence());
}
}
// 返回“升序EntrySet荟萃”
public Set<Map.Entry<K,V>> entrySet() {
EntrySetView es = entrySetView;
return (es != null) ? es : new AscendingEntrySetView();
}
TreeMap.Entry<K,V> subLowest() { return absLowest(); }
TreeMap.Entry<K,V> subHighest() { return absHighest(); }
TreeMap.Entry<K,V> subCeiling(K key) { return absCeiling(key); }
TreeMap.Entry<K,V> subHigher(K key) { return absHigher(key); }
TreeMap.Entry<K,V> subFloor(K key) { return absFloor(key); }
TreeMap.Entry<K,V> subLower(K key) { return absLower(key); }
}
// 降序的SubMap,担任于NavigableSubMap
// 对比于升序SubMap,它的实现机制是将“SubMap的较量器反转”!
static final class DescendingSubMap<K,V> extends NavigableSubMap<K,V> {
private static final long serialVersionUID = 912986545866120460L;
DescendingSubMap(TreeMap<K,V> m,
boolean fromStart, K lo, boolean loInclusive,
boolean toEnd, K hi, boolean hiInclusive) {
super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
// 反转的较量器:是将原始较量器关键字: