private final Comparator<? super K> comparator;
private transient Entry<K,V> root;
private transient int size = 0;
private transient int modCount = 0;
* Constructs a new, empty tree map, using the natural ordering of its keys
* 只做了一件事,把 comparator 赋值为null
*/
public TreeMap() {
comparator = null;
}
* Node in the Tree. Doubles as a means to pass key-value pairs back to
* user (see Map.Entry).
* Entry 是TreeMap基本数据节点
*/
static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left;
Entry<K,V> right;
Entry<K,V> parent;
boolean color = BLACK;
...
}
* TreeMap 有自己重写的 put 方法,代码有点多。。。
* 理解TreeMap的前提是掌握“红黑树”。
* 若理解“红黑树中添加节点”的算法,则很容易理解put。
*/
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key);
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<K,V> parent;
Comparator<? super K> cpr = comparator;
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();
@SuppressWarnings("unchecked")
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);
}
Entry<K,V> e = new Entry<>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
* 接下来看看lastKey()
*/
public K lastKey() {
return key(getLastEntry());
}
static <K> K key(Entry<K,?> e) {
if (e==null)
throw new NoSuchElementException();
return e.key;
}
* Returns the last Entry in the TreeMap (according to the TreeMap's
* key-sort function). Returns null if the TreeMap is empty.
*/
final Entry<K,V> getLastEntry() {
Entry<K,V> p = root;
if (p != null)
while (p.right != null)
p = p.right;
return p;
}
* 再看看floorEntry,返回不大于key的最大的键值对,没有的话返回null
*/
public Map.Entry<K,V> floorEntry(K key) {
return exportEntry(getFloorEntry(key));
}
* 获取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;
}
* 返回一个拷贝值
*/
static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
return (e == null) ? null :
new AbstractMap.SimpleImmutableEntry<>(e);
}
* 接下来是lowerKey()
*/
public K lowerKey(K key) {
return keyOrNull(getLowerEntry(key));
}
* 这里的getLowerEntry()和getFloorEntry()很像
* 获取TreeMap中小于key的最大的节点。若不存在,就返回null。
* 与getFloorEntry()区别在于排除了等于key的节点
* 最后,看看headMap(),有两个不同类型的,我示例中用到了SortedMap类型的
*/
public SortedMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}
public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
return new AscendingSubMap<>(this,
true, null, true,
false, toKey, inclusive);
}