所有集合基于jdk1.8,对源码稍做调整。
HashMap
主要变量
// 比较器
private final Comparator<? super K> comparator;
// 根节点
private transient Entry<K, V> root;
// TreeMap大小
private transient int size = 0;
// 修改量
private transient int modCount = 0;
构造方法
TreeMap() {
this.comparator = null;
}
TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
public TreeMap(Map<? extends K, ? extends V> m) {
comparator = null;
putAll(m);
}
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) {
}
}
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);
}
//比较器不存在,调用key的compare方法
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;
}
/**
* 红黑树实现
* @param x
*/
private void fixAfterInsertion(Entry<K, V> x) {
//先设为红色节点
x.color = RED;
//父颜色P为红时,进行变换(红黑树不允许分支上连续出现红色节点)
while (x != null && x != root && x.parent.color == RED) {
//当P在其父节点G左边时
if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
//获取G的右子树Y
Entry<K, V> y = rightOf(parentOf(parentOf(x)));
//Y为红,设P为红,Y为黑,G为红,G变换为X继续迭代
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
x = parentOf(parentOf(x));
}
//Y为黑
else {
//X在右边时,X转换为父节点并进行左旋
if (x == rightOf(parentOf(x))) {
x = parentOf(x);
rotateLeft(x);
}
//将X父节点设为黑色,父节点的父节点G设为红色,并将G右旋
setColor(parentOf(x), BLACK);
setColor(parentOf(parentOf(x)), RED);
rotateRight(parentOf(parentOf(x)));
}
} else {
Entry<K, V> y = leftOf(parentOf(parentOf(x)));
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
x = parentOf(parentOf(x));
} else {
if (x == leftOf(parentOf(x))) {
x = parentOf(x);
rotateRight(x);
}
setColor(parentOf(x), BLACK);
setColor(parentOf(parentOf(x)), RED);
rotateLeft(parentOf(parentOf(x)));
}
}
}
root.color = BLACK;
}
get
public V get(Object key) {
Entry<K, V> p = getEntry(key);
return (p == null ? null : p.value);
}
final Entry<K, V> getEntry(Object key) {
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
@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;
}
TreeSet
TreeSet内部维护了一个TreeMap,元素不重复也是依靠TreeMap的key值不重复实现的。
主要变量
//实际元素
private transient NavigableMap<E, Object> m;
//充当每个key的对应value
private static final Object PRESENT = new Object();
网友评论