LinkedHashMap

1.類聲明:

//使用Hash表和雙向列表存放數據
public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>
{}

2.變量:

//繼承HashMap的節點同時增加前後節點屬性
 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;
      //訪問順序,按插入順序或是訪問順序(get方法調用順序),如果按訪問順序,則調用get,put後將節點移到雙向列表尾端
       final boolean accessOrder;

3.方法:

//把節點移動到雙向列表最尾部,LinkedHashMap相比較HashMap的最大區別就是內部維持了一個雙向列表,put和get方法中調用了此方法來保證accessOrder爲true時列表中節點爲LRU順序,最近最少使用的放在鏈表頭
    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;
    }

參考文章:http://blog.csdn.net/u012403290/article/details/70143443?locationNum=14&fps=1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章