ew NullPointerException();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
// 从根节点开始,不断比较key的大小进行查找
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0) // 小于,转向左子树
p = p.left;
else if (cmp > 0) // 大于,转向右子树
p = p.right;
else
return p;
}
return null; // 没有相等的key,返回null
}
而插入和删除操作与平衡二叉查找树的细节是息息相关的,关于红黑树的实现细节,我之前写过的一篇博客红黑树的那点事儿已经讲的很清楚了,对这方面不了解的读者建议去阅读一下,就不在这里重复叙述了。
集合视图
最后看一下TreeMap的集合视图的实现,集合视图一般都是实现了一个封装了当前实例的类,所以对集合视图的修改本质上就是在修改当前实例,TreeMap也不例外。
TreeMap的headMap()
、tailMap()
以及subMap()
函数都返回了一个静态内部类AscendingSubMap,从名字上也能猜出来,为了支持倒序,肯定也还有一个DescendingSubMap,它们都继承于NavigableSubMap,一个继承AbstractMap并实现了NavigableMap的抽象类:
abstract static class NavigableSubMap<K,V> extends AbstractMap<K,V>
implements NavigableMap<K,V>, java.io.Serializable {
private static final long serialVersionUID = -2102997345730753016L;
final TreeMap<K,V> m;
/**
* (fromStart, lo, loInclusive) 与 (toEnd, hi, hiInclusive)代表了两个三元组,
* 如果fromStart为true,那么范围的下限(绝对)为map(被封装的TreeMap)的起始key,
* 其他值将被忽略。
* 如果loInclusive为true,lo将会被包含在范围内,否则lo是在范围外的。
* toEnd与hiInclusive与上述逻辑相似,只不过考虑的是上限。
*/
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;
}
// internal utilities
final boolean tooLow(Object key) {
if (!fromStart) {
int c = m.compare(key, lo);
// 如果key小于lo,或等于lo(需要lo不包含在范围内)
if (c < 0 || (c == 0 && !loInclusive))
return true;
}
return false;
}
final boolean tooHigh(Object key) {
if (!toEnd) {
int c = m.compare(key, hi);
// 如果key大于hi,或等于hi(需要hi不包含在范围内)
if (c > 0 || (c == 0 && !hiInclusive))
return true;
}
return false;
}
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);
}
// 判断key是否在该视图的范围之内
final boolean inRange(Object key, boolean inclusive) {
return inclusive ? inRange(key) : inClosedRange(key);
}
/*
* 以abs开头的函数为关系操作的绝对版本。
*/
/*
* 获得最小的键值对:
* 如果fromStart为true,那么直接返回当前map实例的第一个键值对即可,
* 否则,先判断lo是否包含在范围内,
* 如果是,则获得当前map实例中大于或等于lo的最小的键值对,
* 如果不是,则获得当前map实例中大于lo的最小的键值对。
* 如果得到的结果e超过了范围的上限,那么返回null。
*/
final TreeMap.Entry<K,V> absLowest() {
TreeMap.Entry<K,V> e =
(fromStart ? m.getFirstEntry() :
(loInclusive ? m.getCeilingEntry(lo) :
m.getHigherEntry(lo)));
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))