Java集合-09LinkedHashMap源碼解析及使用實例
LinkedHashMap 簡介
hash表和鏈表實現了map接口,叠代順序是可以預測的。LinkedHashMap和HashMap的不同是它所有的entry
維持了一個雙向鏈表結構。該鏈表定義了通常叠代順序是鍵插入的順序。
LinkedHashMap 定義
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
繼承HashMap類,表明對於HashMap的操作LinkedHashMap都支持
LinkedHashMap 結構圖
屬性解析
- transient LinkedHashMap.Entry<K,V> head;//鏈表頭部
- transient LinkedHashMap.Entry<K,V> tail;//鏈表尾部
- final boolean accessOrder;//標明LinkedHashMap叠代順序:true代表按照最近使用排序,false表示按照插入順序排序
LinkedHashMap 構造函數
public LinkedHashMap()
創建一個初始容量為16,加載因子為0.75,按照插入順序叠代的Map
public LinkedHashMap(int initialCapacity)
創建一個初始容量為initialCapacity大小,加載因子為0.75,按照插入順序叠代的Map
public LinkedHashMap(int initialCapacity, float loadFactor)
創建一個初始容量為initialCapacity大小,加載因子為loadFactor,按照插入順序叠代的Map
public LinkedHashMap(Map<? extends K, ? extends V> m)
創建一個加載因子為0.75,包含m的鍵值對的Map
public LinkedHashMap(int initialCapacity,float loadFactor,boolean accessOrder)
創建一個初始容量為16,加載因子為0.75,accessOrder為true時為使用按照最近使用順序叠代,false為插入順序叠代的Map
LinkedHashMap 源碼分析
- Entry
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中Entry多了兩個屬性,before和after,從而維護了一個雙向鏈表,
同時類中的head和tail分別指向鏈表的頭部和尾部
put
LinkedHashMap沒有重寫HashMap中的put方法。但是重寫了構建新節點的newNode()方法,該方法會在
HashMap的putVal()中調用
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) {//p添加到雙向鏈表的尾部
LinkedHashMap.Entry<K,V> last = tail;
tail = p;//鏈表最後一個節點指向新增的Entry
if (last == null)//如果原來鏈表最後一個節點為null,表明雙向鏈表為空
head = p;//雙向鏈表的頭節點指向新增節點p
else {//表明原鏈表不為空
p.before = last;//p的前一個節點指向原鏈表最後一個節點
last.after = p;//原最後一個節點的後一節點指向p
}
}
同時LinkedHashMap重寫了HashMap中void afterNodeAccess(Node<K,V> p) { }和void afterNodeInsertion(boolean evict) { }方法
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMap.Entry<K,V> last;
if (accessOrder && (last = tail) != e) {//對於accessOrder為true時候,將該Node移動到鏈表的最後位置,保證了遍歷時候按照訪問順序叠代
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;
}
}
//需要說明的是這個方法中removeEldestEntry方法在LinkedHashMap是返回false的,故對於後續操作
//沒有意義,通常構建一個LruCache會在達到Cache的上限是返回true
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);
}
}
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}
remove
LinkedHashMap中也沒有重寫remove方法,但是重寫了HashMap中空方法void afterNodeRemoval(Node<K,V> p) { }
//刪除節點e時,刪除鏈表中節點e
void afterNodeRemoval(Node<K,V> e) { // unlink
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.before = p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a == null)
tail = b;
else
a.before = b;
}
get
LinkedHashMap中重寫了get方法
public V get(Object key) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)//查詢時候使用HashMap的getNode方法
return null;
if (accessOrder)//如果accessOrder為true,調整鏈表中對應節點位置,保證遍歷時候按照訪問時間順序叠代
afterNodeAccess(e);
return e.value;
}
遍歷
LinkedHashMap重寫了entrySet方法
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new LinkedEntrySet()) : es;
}
final class LinkedEntrySet extends AbstractSet<Map.Entry<K,V>>{
//省略部分代碼
public final Iterator<Map.Entry<K,V>> iterator() {
return new LinkedEntryIterator();
}
}
final class LinkedEntryIterator extends LinkedHashIterator
implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}
abstract class LinkedHashIterator {
LinkedHashMap.Entry<K,V> next;
LinkedHashMap.Entry<K,V> current;
int expectedModCount;
LinkedHashIterator() {
next = head;
expectedModCount = modCount;
current = null;
}
public final boolean hasNext() {
return next != null;
}
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;
}
public final void remove() {
Node<K,V> p = current;
if (p == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false);
expectedModCount = modCount;
}
}
最終可以看出叠代時候從內部維護的雙向鏈表的表頭開始遍歷,同時雙向鏈表的順序在LinkedHashMap的增、刪、改、查時候都會維護,保證了遍歷時候按照插入順序或者訪問時間順序叠代
Java集合-09LinkedHashMap源碼解析及使用實例