TreeMap继承AbstractMap,实现了NavigableMap, Cloneable, java.io.Serializable接口
//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;
Entry(K key, V value, Entry<K,V> parent) {
this.key = key;
this.value = value;
this.parent = parent;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
}
public int hashCode() {
int keyHash = (key==null ? 0 : key.hashCode());
int valueHash = (value==null ? 0 : value.hashCode());
return keyHash ^ valueHash;
}
public String toString() {
return key + "=" + value;
}
}
内部几个比较重要的方法有getEntry(Object key),put(K key, V value),compare(Object k1, Object k2),deleteEntry(Entry
//TreeMap保持内部的数据排序,用户可以自定义实现Comparable接口的排序,默认使用 comparator.compare方法进行排序。
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
put(K key, V value)
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) //插入的key不能为空
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;
}
getEntry(Object key)
final Entry<K,V> getEntry(Object key) {
// 使用用户自定义排序
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException(); //不允许key=null
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
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;
}
deleteEntry(Entry
private void deleteEntry(Entry<K,V> p) {
modCount++;
size--;
if (p.left != null && p.right != null) {
Entry<K,V> s = successor(p); //返回当前节点的子节点
p.key = s.key;
p.value = s.value;
p = s;
}
//找到左右子树
Entry<K,V> replacement = (p.left != null ? p.left : p.right);
if (replacement != null) {
//存在左子节点或右子节点
replacement.parent = p.parent;
if (p.parent == null)
//根节点
root = replacement;
else if (p == p.parent.left)
//左子节点
p.parent.left = replacement;
else
//右子节点
p.parent.right = replacement;
// 删除节点
p.left = p.right = p.parent = null;
//维护红黑树内部结构
if (p.color == BLACK)
fixAfterDeletion(replacement);
} else if (p.parent == null) {
// 只有一个节点
root = null;
} else { // 没有子节点
if (p.color == BLACK)
//维护红黑树内部结构
fixAfterDeletion(p);
if (p.parent != null) {
if (p == p.parent.left)
p.parent.left = null;
else if (p == p.parent.right)
p.parent.right = null;
p.parent = null;
}
}
}
总结
- TreeMap内部以红黑树结构存储数据,内部排序默认使用Comparable.compareTo()方法,也可以重写comparator.compare()方法进行排序。
- TreeMap不允许key=null。
- 如果插入相同key,不同value值,会把原来的值覆盖并返回。
JDK版本1.8.0_92。