引言
今天看java编程思想的时候读到HashMap以及LinkedHashMap,HashMap比较熟悉经常遇到但LinkedHashMap我不是很了解就去查看了相关原理用途,然后发现可能因为jdk版本不同网上提到最多的就是LinkedHashMap重现了HashMap的三个方法( afterNodeAccess(Node<K,V> p),afterNodeInsertion(boolean evict),afterNodeRemoval(Node<K,V> p)),但我没看到LinkedHashMap是如何实现它在put时候维护双向链表的,有几篇文章提到了但是跟jdk1.8的实现方法还是不同。
注意
这里我就基于jdk1.8来说下LinkedHashMap如何实现put的时候维护双向链表的顺序
数据存储结构
LinkedHashMap 继承于HashMap实现了 Map接口是HashMap的子类,也就是说LinkedHashMap 拥有HashMap的数据存储结构不同的地方在于LinkedHashMap 额外维护了一个双向链表如图:
此图转自:https://blog.csdn.net/u012403290/article/details/70143443?locationNum=14&fps=1 尊重原创。
LinkedHashMap区别于HashMap的地方是由于维护了一个双向链表所以LinkedHashMap可以做到保持顺序(插入顺序或者访问顺序)。
通过一个例子来说下LinkedHashMap是如何维护双向链表的:
public class Test {
public static void main(String[] args) {
Map<String, String> map =new LinkedHashMap<String,String>();
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("11", "1");
map.put("22", "2");
map.put("4", "3");
System.out.println(map);
}
}//{1=1, 2=2, 3=3, 11=1, 22=2, 4=3}
可以看出输出的顺序是按put的顺序进行输出的。LinkedHashMap是如何做到的呢,就需要看下它的源码了。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);//LinkedHashMap重写了newNode方法
else {
Node<K,V> e; K k;
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);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
该put方法为HashMap的put方法,LinkedHashMap继承HashMap后并没有重写此put和putVal方法,而是重写了newNode方法、afterNodeAccess和afterNodeInsertion三个方法。
newNode方法源码:
Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
LinkedHashMap.Entry<K,V> p =
new LinkedHashMap.Entry<K,V>(hash, key, value, e);
linkNodeLast(p);
return p;
}
private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
LinkedHashMap.Entry<K,V> last = tail;
tail = p;
if (last == null)
head = p;
else {
p.before = last;
last.after = p;
}
}
可以看出通过重写newNode方法来保证LinkedHashMap新生成Node可以插入到双向链表的尾部。这里的Entry是继承与HashMap的Node添加了before和after指向前节点和后节点变量。
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
到这里就解开了我的困惑,LinkedHashMap是如何在put后保持双向链表的顺序的,至于其他方法如重写三个方法以及accessorder默认false为插入顺序true为访问顺序的原理等等,网上博客有很多讲解, 我就不再重复了。