首先实现map的子类:HashMap、HashTable、TreeMap、LinkedHashMap。
这几个类中HashMap与HashTable的区别:
线程上:HashMap是线程不安全的,而HashTable是安全的。key、value的支持:HashMap的key、balue可以为空,而HashTable是key、value不可以为空的。底层数据结构:HashMap采用数组+链表+红黑树,当链表的长度>=8的时候会考虑是否转成红黑树,而HashTable则没有。初始化容量上:HashTable的初始化容量是11,同时扩容变为原来的2n+1,HashMap的初始化容量是16,同时扩容扩容成原来的2倍。而当给定初始化容量时,HashTable是直接给定初始化容量,而HashMap是将给定的初始化容量变成2的次幂。
jdk7和jdk8之间,jdk8引入了红黑树,同时jdk7中的transform( )方法变成了resize( )方法。什么是红黑树,同时数组什么时候会加入链表,链表和数组满足什么条件时,链表会变成红黑树,什么时候红黑树又会变成链表。同时如果给定初始化容量,给成怎样会才合理。
1.HashMap相关变量
//默认初始化容量2^4 =16,做左移位运算
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//最大容量 2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认加载因子 0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//需要变成红黑树的链表阀值 8
static final int TREEIFY_THRESHOLD = 8;
//将红黑树变成链表的阀值 6
static final int UNTREEIFY_THRESHOLD = 6;
//变成红黑树除了满足链表达到8,而且数组需要
//达到64,最小转成红黑树的数组长度64
static final int MIN_TREEIFY_CAPACITY = 64;
//数组,又称为bucket,桶,2的次幂
transient Node<K,V>[] table;
//entrySet:k、v对的集合
transient Set<Map.Entry<K,V>> entrySet;
//map的长度
transient int size;
//修改次数
transient int modCount;
//阀值 threshold = capacity*loadFactor
int threshold;
//加载因子
final float loadFactor;
2.构造函数
//构造函数,对传入的加载因子和初始化容量校验
public HashMap(int initialCapacity, float loadFactor) {
//如果初始容量<0,则抛异常
if (initialCapacity < 0)
throw new IllegalArgumentException(
"Illegal initial capacity: " + initialCapacity);
//如果初始容量>最大容量,则将最大容量赋值给初始化容量
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
//加载因子如果小于0或者加载因子为空,则抛异常
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException(
"Illegal load factor: " + loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
//将传入的容量变成2的n次幂
static final int tableSizeFor(int cap) {
//向减去1,比如避免16变成32
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
//之后判断如果大于最大值,变成最大值,
//否者变成n+1,也即变成2的次幂的形式
return (n < 0) ? 1 : (
n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
//构造hashMap,传入初始化容量,默认的加载因子
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
//构造hash,加载因子默认,容量默认
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
//构造一个HashMap:加载因子为默认加载因子
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
//放入传入的map信息、
putMapEntries(m, false);
}
final void putMapEntries(Map<? extends K, ?
extends V> m, boolean evict) {
//拿到map为m的长度
int s = m.size();
//如果长度>0,则判断是否为空
if (s > 0) {
if (table == null) { // pre-size
//拿到ft=长度/加载因子+1
float ft = ((float)s / loadFactor) + 1.0F;
//如果ft<最大容量,则为ft合理的长度
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
//如果t>阀值,则需要进行初始化,变成2的次幂,
// 类似于扩容,考虑到为变成16的情况
if (t > threshold)
threshold = tableSizeFor(t);
}
//否者,s>阀值,则会进行扩容操作
else if (s > threshold)
resize();
//拿到key、value的值,执行put操作
for (Map.Entry<? extends K, ?
extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
//进行put操作,添加元素信息
putVal(hash(key), key, value, false,evict);
}
}
}
3.方法
put方法
public V put(K key, V value) {
return putVal(hash(key),
key, value, false, true);
}
//注意到onlyIfAbsent为false,则可以进行值的替换,
// 如果设置为true,则不可以进行修改value的值
final V putVal(int hash, K key, V value,
boolean onlyIfAbsent,boolean evict) {
//定义变量
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果table(数组)为空,则进行resize()进行初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//(n-1)&hash,相当于对其进行取模,放入到哪个数组中
if ((p = tab[i = (n - 1) & hash]) == null)
//放入到数组中
tab[i] = newNode(hash, key, value, null);
//如果不为空
else {
Node<K,V> e; K k;
//如果key存在同时hash值相同,
//也即数组中已经有元素存在时,
//则更新value的值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果元素属于树节点,则进行红黑树的判断,执行红黑树操作
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//否者执行链表操作,元素在链表中
else {
//遍历链表
for (int binCount = 0; ; ++binCount) {
//遍历到链表尾部
if ((e = p.next) == null) {
//为元素创建信息节点
p.next = newNode(hash, key, value, null);
//如果binCount个数,也即链表个数>=8时,则进行红黑树操作
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//执行红黑树操作,首先会去判断数组的
// 长度是否>=64,执行完进行断开
treeifyBin(tab, hash);
break;
}
//如果待插入的key在链表中找到,则退出循环
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//与前面的e = p.next构成循环
p = e;
}
}
//如果e存在,则将新值替换旧值
if (e != null) { // existing mapping for key
V oldValue = e.value;
//判断是否替换成新值
if (!onlyIfAbsent || oldValue == null)
//替换旧值为新值
e.value = value;
//回调函数,留给LInkedHashMap实现
afterNodeAccess(e);
return oldValue;
}
}
//修改次数自增
++modCount;
//size>阀值,进行扩容操作
if (++size > threshold)
resize();
//回调函数,留给LinkedHashMap实现
afterNodeInsertion(evict);
return null;
}
/**
* Tree version of putVal.
*/
//红黑树的put放入方法
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
//获取根节点
TreeNode<K,V> root = (parent != null) ? root() : this;
//从根节点进行遍历
for (TreeNode<K,V> p = root;;) {
int dir, ph; K pk;
//hash值下小于父节点的hash值,则取左节点
if ((ph = p.hash) > h)
dir = -1;
//如果hash值大于父节点hash值,则取右节点
else if (ph < h)
dir = 1;
//如果hash相等,继续比较键值是否相等,
//如果相等则说明存在相同的键值,则直接返回当前节点的值
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
//如果hash值相等,但是key不相等
else if ((kc == null &&
//key没有实现Comparable比较接口
(kc = comparableClassFor(k)) == null) ||
//或者已经实现Comparable比较接口,同时等于0
//则不能进行key的顺序判断,因此无法判断,会产生hash碰撞
(dir = compareComparables(kc, k, pk)) == 0) {
//判断是否执行过查找方法
if (!searched) {
TreeNode<K,V> q, ch;
//如果执行过,则分别查找左右树,查看是否有相同的对象,
//如果找不到,则说明确实没有找到相同对象存在
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
//hash相同,key不相等,并且key没有实现Comparable接口,
//保持一致的处理规则,以便进行排序,从而进行遍历
dir = tieBreakOrder(k, pk);
}
//备份当前节点
TreeNode<K,V> xp = p;
//如果遍历完之后还是没有找到相同的key的节点,则进行新的插入操作
if ((p = (dir <= 0) ? p.left : p.right) == null) {
Node<K,V> xpn = xp.next;
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
//备份当前节点的下一个节点;给LinkedHashMap预留的
if (dir <= 0)
xp.left = x;
else
xp.right = x;
//将当前节点的next节点指向新节点
xp.next = x;
//设置新的父节点和当前节点为当前节点
x.parent = x.prev = xp;
//如果当前节点还是后继节点,
// 则将新节点作为当前节点的前驱节点
if (xpn != null)
((TreeNode<K,V>)xpn).prev = x;
//实现红黑树重排实现重平衡
moveRootToFront(tab, balanceInsertion(root, x));
return null;
}
}
}
/**
* Replaces all linked nodes in bin at index for given hash unless
* table is too small, in which case resizes instead.
*/
//进行数组长度64的判断,如果成立,则将链表转换为红黑树
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
//如果数组长度<64,则进行扩容操作
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
//否者进行红黑树操作
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
//创建红黑树节点
TreeNode<K,V> p = replacementTreeNode(e, null);
//将第一个节点作为头结点,并取出原链表的元素,重排成双向链表
if (tl == null)
hd = p;
else {
//否者进行前驱和后继操作
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
//如果根节点不为空,则进行红黑树的重平衡操作
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
/**
* Forms tree of the nodes linked from this node.
* @return root of tree
*/
//进行重平衡操作
final void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
}
}
moveRootToFront(tab, root);
}
/**
* Ensures that the given root is the first node of its bin.
*/
//将root节点放到哈希表的首元素
static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
int n;
if (root != null && tab != null && (n = tab.length) > 0) {
int index = (n - 1) & root.hash;
TreeNode<K,V> first = (TreeNode<K,V>)tab[index];
if (root != first) {
Node<K,V> rn;
tab[index] = root;
TreeNode<K,V> rp = root.prev;
if ((rn = root.next) != null)
((TreeNode<K,V>)rn).prev = rp;
if (rp != null)
rp.next = rn;
if (first != null)
first.prev = root;
root.next = first;
root.prev = null;
}
assert checkInvariants(root);
}
}
//树节点查找
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
//进行do-while循环找到相要的元素
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
//hash值小于当前节点,则在左节点进行比较,否者在右节点比较
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
//否者是当前的节点,直接返回
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
//左节点为空,则遍历右节点
else if (pl == null)
p = pr;
//右节点为空,则遍历左节点
else if (pr == null)
p = pl;
//如果实现了Comparable接口,则更加compareTo比较出来的大小获取顺序
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
//如果没有则采用find()放到进行返回
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
扩容操作
//进行扩容操作
final Node<K,V>[] resize() {
//老的数组
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
//首先老的容量是否大于0,如果大于0,
//则进行最大容量判断,如果最大容量判断不为空,
// 则将阀值设置为最大容量,返回老容量
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//否者对新容量进行做位运算左移一位,扩容2倍
//如果小于最大容量,同时老容量大于默认初始化容量,
//则阀值扩容成原来的2倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
//如果老阀值大于0,则新容量等于老阀值
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else {//否者没有进行初始容量,则 采用默认容量16,新的阀值为16*0.75
// zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//如果新的阀值为0,则采用默认的
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
//采用新的阀值
threshold = newThr;
//创建一个新的数组,同时 newCap = 0,方便扩容使用
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
//数组等于新的tab
table = newTab;
//原来的hash表不为空时,将原来的元素复制到新的hash表中
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
//将原来的元素置空
oldTab[j] = null;
//如果当前元素的后继为空,将e放入到取模的新数组中
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
//如果为树节点,则进行红黑树的扩容操作
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//保持顺序
else { // preserve order
//lohead指low的低位的头结点,loTail表示低位的尾节点
Node<K,V> loHead = null, loTail = null;
//hihead指low的高位的头结点,hiTail表示高位的尾节点
Node<K,V> hiHead = null, hiTail = null;
//下一个节点
Node<K,V> next;
//取模只可能出现0或者1的情况
do {
//e元素的前驱节点
next = e.next;
//如hash值对老容量取余为0,则放入到低位的链表中
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
//否者则说明hash值对老容量取模不为0,
//也就是为1的情况,放在高位的链表中
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
//遍历完成分化两个链表
//低位链表在新的哈希表中的位置与旧哈希表一样
loTail.next = null;
newTab[j] = loHead;
}
//高位链表在新哈希表中的位置为原来的位置+oldcap
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
//将树中的节点拆分为较高和较低的树,
//如果现在太小,则取消树化。仅从调整
//大小调用。也即进行树变链表和链表转成树的操作
final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
//获取当前元素
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
//低位、高位的头结点和尾节点
TreeNode<K,V> loHead = null, loTail = null;
TreeNode<K,V> hiHead = null, hiTail = null;
int lc = 0, hc = 0;
//从根节点开始遍历红黑树,统计出需要放到低位和需要移动到高位的元素个数
//进行红黑树操作或者红黑树转链表操作
for (TreeNode<K,V> e = b, next; e != null; e = next) {
next = (TreeNode<K,V>)e.next;
e.next = null;
if ((e.hash & bit) == 0) {
if ((e.prev = loTail) == null)
loHead = e;
else
loTail.next = e;
loTail = e;
++lc;
}
else {
if ((e.prev = hiTail) == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
++hc;
}
}
//如果低位头结点不为空,则进行低位操作
if (loHead != null) {
//小于红黑树阀值时,则不满足红黑树操作,转链表
if (lc <= UNTREEIFY_THRESHOLD)
tab[index] = loHead.untreeify(map);
//否者进行红黑树操作,将低位头结点放入到hash表中
else {
tab[index] = loHead;
//如果高位头结点不为空,则进行重平衡操作
if (hiHead != null) // (else is already treeified)
loHead.treeify(tab);
}
}
//如果高位头结点不为空,则进行高位操作,
//如果不满足红黑树阀值,则进行转链表操作
//否者进行红黑树重平衡操作
if (hiHead != null) {
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
hiHead.treeify(tab);
}
}
}
/**
* Returns a list of non-TreeNodes replacing those linked from
* this node.
*/
//将树转成链表
final Node<K,V> untreeify(HashMap<K,V> map) {
Node<K,V> hd = null, tl = null;
for (Node<K,V> q = this; q != null; q = q.next) {
Node<K,V> p = map.replacementNode(q, null);
if (tl == null)
hd = p;
else
tl.next = p;
tl = p;
}
return hd;
}
remove操作
//进行删除操作
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
//删除节点信息
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
//如果hash表、表长度不为空,同时hash取模不为空,则进行移除操作
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
//找到该元素进行存放
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
//如果当前节点的下一个节点不为空,则有可能为树节点,或者链表
else if ((e = p.next) != null) {
//树节点,拿到该元素信息
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
//链表,拿到该元素
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
//拿到之后,进行删除操作
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
//树节点查找
final TreeNode<K,V> getTreeNode(int h, Object k) {
return ((parent != null) ? root() : this).find(h, k, null);
}
//树节点查找
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
//进行do-while循环找到相要的元素
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
//hash值小于当前节点,则在左节点进行比较,否者在右节点比较
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
//否者是当前的节点,直接返回
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
//左节点为空,则遍历右节点
else if (pl == null)
p = pr;
//右节点为空,则遍历左节点
else if (pr == null)
p = pl;
//如果实现了Comparable接口,则更加compareTo比较出来的大小获取顺序
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
//如果没有则采用find()放到进行返回
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
//进行红黑树的删除操作
final void removeTreeNode(HashMap<K,V> map, Node<K,V>[] tab,
boolean movable) {
int n;
//进行判空操作,如果为空,则直接返回
if (tab == null || (n = tab.length) == 0)
return;
//进行取余操作
int index = (n - 1) & hash;
TreeNode<K,V> first = (TreeNode<K,V>)tab[index], root = first, rl;
//下一个元素 pred = prev为前驱
TreeNode<K,V> succ = (TreeNode<K,V>)next, pred = prev;
//前驱节点为空,则说明为头结点,执行将位置指向后元素
if (pred == null)
tab[index] = first = succ;
//否者,则说明头结点不为空,则进行前驱节点的下一个节点指向后节点
else
pred.next = succ;
//如果后节点不为空,则将后节点的前一个节点指向前节点
if (succ != null)
succ.prev = pred;
//如果first为空,则说明没有元素,直接返回
if (first == null)
return;
//root还有父节点,则说明不是根,拿到根
if (root.parent != null)
root = root.root();
if (root == null || root.right == null ||
(rl = root.left) == null || rl.left == null) {
//红黑树转链表操作
tab[index] = first.untreeify(map); // too small
return;
}
//p为删除节点,pl为左节点,pr为有节点,replacement为要移动的子节点
TreeNode<K,V> p = this, pl = left, pr = right, replacement;
//左右节点 不为空
if (pl != null && pr != null) {
//s为右节点
TreeNode<K,V> s = pr, sl;
//s的右节点的左节点不为空
while ((sl = s.left) != null) // find successor
s = sl;
boolean c = s.red; s.red = p.red; p.red = c; // swap colors
//交换删除节点和右节点的左节点的颜色,进行变色操作
TreeNode<K,V> sr = s.right;
TreeNode<K,V> pp = p.parent;
if (s == pr) { // p was s's direct parent
p.parent = s;
s.right = p;
}
else {
TreeNode<K,V> sp = s.parent;
if ((p.parent = sp) != null) {
if (s == sp.left)
sp.left = p;
else
sp.right = p;
}
if ((s.right = pr) != null)
pr.parent = s;
}
p.left = null;
if ((p.right = sr) != null)
sr.parent = p;
if ((s.left = pl) != null)
pl.parent = s;
if ((s.parent = pp) == null)
root = s;
else if (p == pp.left)
pp.left = s;
else
pp.right = s;
if (sr != null)
replacement = sr;
else
replacement = p;
}
//pl不为空,则删除节点为左节点
else if (pl != null)
replacement = pl;
//pr不为空,则删除节点为右节点
else if (pr != null)
replacement = pr;
//删除元素为叶子节点
else
replacement = p;
//如果删除节点后进行红黑树处理,使用变色和旋转
if (replacement != p) {
TreeNode<K,V> pp = replacement.parent = p.parent;
if (pp == null)
root = replacement;
else if (p == pp.left)
pp.left = replacement;
else
pp.right = replacement;
p.left = p.right = p.parent = null;
}
TreeNode<K,V> r = p.red ? root : balanceDeletion(root, replacement);
//删除节点为叶子节点,则删除与之关联的节点
if (replacement == p) { // detach
TreeNode<K,V> pp = p.parent;
p.parent = null;
if (pp != null) {
if (p == pp.left)
pp.left = null;
else if (p == pp.right)
pp.right = null;
}
}
//根据标识决定是否将根节点转移hash表首元素
if (movable)
moveRootToFront(tab, r);
}
get操作
//通过key获取value的信息
public V get(Object key) {
//节点信息,返回节点的值
Node<K,V> e;
//获取value值
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
//通过hash值和key,拿到节点信息,进一步拿到value值
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//哈希表不为空,同时长度大于0,hash取模不为空时
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//进行判断首节点的hash、key是否与之相同
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//如果首节点有后继节点,则说明有链表或者红黑树,此时需要进行判盘
//如果为树节点,则进行树节点信息的获取,否者为链表,遍历链表获取元素
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
final TreeNode<K,V> getTreeNode(int h, Object k) {
return ((parent != null) ? root() : this).find(h, k, null);
}
//树节点查找
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
//进行do-while循环找到相要的元素
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
//hash值小于当前节点,则在左节点进行比较,否者在右节点比较
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
//否者是当前的节点,直接返回
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
//左节点为空,则遍历右节点
else if (pl == null)
p = pr;
//右节点为空,则遍历左节点
else if (pr == null)
p = pl;
//如果实现了Comparable接口,则更加compareTo比较出来的大小获取顺序
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
//如果没有则采用find()放到进行返回
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
//比较器
static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c;
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
if (((t = ts[i]) instanceof ParameterizedType) &&
((p = (ParameterizedType)t).getRawType() ==
Comparable.class) &&
(as = p.getActualTypeArguments()) != null &&
as.length == 1 && as[0] == c) // type arg is c
return c;
}
}
}
return null;
}
//比较器
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
static int compareComparables(Class<?> kc, Object k, Object x) {
return (x == null || x.getClass() != kc ? 0 :
((Comparable)k).compareTo(x));
}
从里面可以收获到的一些结论:
HashMap的源码较多,且综合了数组、链表、红黑树的操作,因此不管是get还是remove还是get都需要考虑到三张数据结构的操作。同时红黑树是一种平衡二叉树,节点是黑色或者红色的,根节点为黑色的,每个红色节点的两个叶子节点都是黑色的,从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。同时当key相同时,会出现冲突,此时就需要解决hash冲突,此时就将其放入到链表中。而当链表的长度>=8时,数组长度>=64时,则变成红黑树。给定初始化容量时,给定元素个数/加载因子为最佳初始化容量,可以在源码找到这个代码。