一、概述
LinkedHashMap其实是HashMap的一个子类,实现原理其实跟HashMap类似。只不过LinkedHashMap会记录插入key-value的顺序,并且在遍历的时候,会按照插入key-value对的顺序遍历出来。
LinkedHashMap底层是基于双向链表来实现的,底层就是双向链表来记录元素添加的顺序的。
二、源码分析
先看下LinkedHashMap类有哪些属性:
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的节点使用的是这个数据结构。可以看的出来,他在原有的HashMap.Node数据结构基础上再增加了两个指针before, after,是一种双向链表的结构。
put方法用的就是HashMap实现的put方法。
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);
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;
}
最后我们看到有一个newNode(hash, key, value, null)这个方法,这个方法在HashMap中是空实现。在LinkedHashMap中的实现为:
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;
}
}
可以看到这里是创建一个LinkedHashMap.Entry节点返回,并且,把这个节点挂到自己维护的一个链表的尾部。
覆盖,如果是你再次将某个key的值覆盖一下,会怎么样呢?或者取出对应的key的值会怎么样?链表顺序会发生改变吗
首先看get方法实现:
public V get(Object key) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)
return null;
if (accessOrder)
afterNodeAccess(e);
return e.value;
}
还以HashMap的put方法代码我们看到在节点覆盖的时候,也会执行afterNodeAccess(e)这个方法,看下这个方法在LinkedHashMap的实现:
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMap.Entry<K,V> last;
// 这里的意思是accessOrder = true并且e不是在双线链表的尾部,
// 那么就要删除e这个节点,并且把e这个节点挂到链表的尾部
if (accessOrder && (last = tail) != e) {
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a != null)
a.before = b;
else
last = b;
if (last == null)
head = p;
else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}
从上面的代码可以看出,顺序是否发生改变,跟accessOrder 这个属性值有关,默认的事false,表示默认情况下get或者覆盖是不改变顺序的。如果将accessOrder 设置成了true,修改一个key的值,或者是get访问一下这个key,都会导致这个key挪动到链表的尾部去。
在迭代的时候,LinkedHashMap里面会从链表的头部开始迭代,这样通过这个链表就可以维持他的一个顺序
final class LinkedEntryIterator extends LinkedHashIterator
implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}
final LinkedHashMap.Entry<K,V> nextNode() {
LinkedHashMap.Entry<K,V> e = next;
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
current = e;
next = e.after;
return e;
}