LinkedHashMap和HashMap经常会一同问到,不过看了LinkedHashMap内部的源码相对比较简单,如果事先看过HashMap的源码的话。
概览
首先可以看到LinkedHashMap是继承HashMap的,所有HashMap能干的事,LinkedHashMap也能干。
然后多个几个属性,解释如下
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{/**
HashMap的节点增加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);
}
} /**
* 双向链表的头结点
*/
transient LinkedHashMap.Entry<K,V> head; /**
* 双向链表的尾节点
*/
transient LinkedHashMap.Entry<K,V> tail; /**
* 双向链表的访问顺序,true的话房屋有序,false时插入有序。
*/
final boolean accessOrder;
...
}从上面大概可以看出来LinkedHashMap是一个双向链表加Map。在HashMap基础上多个头指针,尾指针,然后节点相互连接
image.png
get操作
看源码
public V get(Object key) {
Node<K,V> e; //如果未获取到元素,返回null
if ((e = getNode(hash(key), key)) == null) return null; //如果获取到了元素,判断accessOrder,如果为true代表按访问排序。
if (accessOrder)
afterNodeAccess(e); return e.value;
}
//把参数中的节点移动至链表的尾节点
void afterNodeAccess(Node<K,V> e) {
LinkedHashMap.Entry<K,V> last; //last节点为尾节点
if (accessOrder && (last = tail) != e) {
//先得到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;
}
}画的比较丑,但是链表的删除操作,大家懂的,O(1),改变一下前后节点的指针即可。
image.png
put操作
put操作,采用的仍然为HashMap原本的操作,不过扩展了节点插入之后操作的方法
put节点的时候,新的节点会维持一个链表。
//覆盖了原本的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;
}
}//HashMap方法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;
} //重写remoeEldestEntry可实现LRU算法
void afterNodeInsertion(boolean evict) { // possibly remove eldest
LinkedHashMap.Entry<K,V> first; if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}resize方法
resize与HashMap没有变化,这里温故一下, 参考之前HashMap的resieze
扩容两倍之后,会重新计算Hash。
假设原本容量为:
16 = 0x10000 ,那么16以下的数与16进行hash都得0. 16以上的数&16为正数
扩容之后,容量变为32,需要重新分配位置的为大于原本16的数,然后加上新的扩容量计算得新的位置。
32 = 0x100000
最后
Java基础中的集合类,有必要掌握
参考:HashMap实现原理