LinkedHashMap雙向連結串列
阿新 • • 發佈:2019-02-12
雙鏈迴環迴圈連結串列,名字相當唬人。首尾相接才算迴圈,但1.8這個沒發現有首尾相接,LinkedList也沒有
LinkedHashMap,有兩種排序,一種是順序儲存排序,像佇列,先放入的元素在隊頭,後放入的元素在隊尾,移除操作在隊頭進行。另一種是訪問排序,被訪問的元素放在隊尾,沒被訪問的放隊頭,put get這些操作都算是訪問
1.順序儲存排序
LinkedHashMap繼承了HashMap,比其多了兩個屬性head 和tail,加了一種連結串列的資料
//重寫的HashMap的newNode方法,除了hashmap的方法外,加了把節點放在尾端public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> { static class Entry<K,V> extends HashMap.Node<K,V> { //繼承了HashMap內部節點類,加了兩個屬性before上個節點,after和下個節點 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;//尾節點 /** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ final boolean accessOrder; //如果是false,則用儲存排序,true用訪問排序,LinkedHashMap的構造方法預設都是false 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; } }
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;
}
LinkedHashMap其實沒有重寫HashMap的put方法,它並有放棄HashMap的雜湊連結串列,只是在這個基礎上加上了自己的連結串列。 來看HashMap的原始碼,put方法呼叫了newNode方法,LinkedHashMap只需要重寫此方法就可以在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;
}
/* ------------------------------------------------------------ */原始碼特地註釋了 以下是支援LinekdHashMap的
// LinkedHashMap support
/*
* The following package-protected methods are designed to be
* overridden by LinkedHashMap, but not by any other subclass.
* Nearly all other internal methods are also package-protected
* but are declared final, so can be used by LinkedHashMap, view
* classes, and HashSet.
*/
// Create a regular (non-tree) node
Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
return new Node<>(hash, key, value, next);
}
LinkedHashMap put先呼叫HashMap的put ,在put中呼叫了LinkedHashMap的newNode方法(把元素插入雙向連結串列中)
來看看LinkedHashMap的remove方法:先呼叫HashMap的remove方法,此方法中呼叫了一個afterNodeRemoval方法,HashMap的afterNodeRemoval是空方法,LinkedHashMap重寫了此方法,把元素從雙向連結串列中移除
HashMap:
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
....省略
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;
}
}
// Callbacks to allow LinkedHashMap post-actions
void afterNodeRemoval(Node<K,V> p) { }
LinkeHashMap的afterNodeRemoval把元素移除
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;
}
2.訪問排序
想使用訪問排序,需要在初始化LinkedHashMap時設定accessOrder為true
看看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;
}
void afterNodeAccess(Node<K,V> e) { // move node to last 把被訪問的元素 放到隊尾
LinkedHashMap.Entry<K,V> last;
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;
}
}