LinkedHashMap 底層分析

衆所周知 HashMap 是一個無序的 Map,由於每次根據 keyhashcode 映射到 Entry 數組上,因此遍歷出來的順序並非寫入的順序。java

所以 JDK 推出一個基於 HashMap 但具備順序的 LinkedHashMap 來解決有排序需求的場景。git

它的底層是繼承於 HashMap 實現的,由一個雙向鏈表所構成。github

LinkedHashMap 的排序方式有兩種:數組

  • 根據寫入順序排序。
  • 根據訪問順序排序。

其中根據訪問順序排序時,每次 get 都會將訪問的值移動到鏈表末尾,這樣重複操做就能的到一個按照訪問順序排序的鏈表。bash

數據結構

@Test
	public void test(){
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		map.put("1",1) ;
		map.put("2",2) ;
		map.put("3",3) ;
		map.put("4",4) ;
		map.put("5",5) ;
		System.out.println(map.toString());

	}
複製代碼

調試能夠看到 map 的組成:數據結構

打開源碼能夠看到:併發

/** * The head of the doubly linked list. */
    private transient Entry<K,V> header;

    /** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */
    private final boolean accessOrder;
    
    private static class Entry<K,V> extends HashMap.Entry<K,V> {
        // These fields comprise the doubly linked list used for iteration.
        Entry<K,V> before, after;

        Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
            super(hash, key, value, next);
        }
    }  
複製代碼

其中 Entry 繼承於 HashMapEntry,並新增了上下節點的指針,也就造成了雙向鏈表。ide

還有一個 header 的成員變量,是這個雙向鏈表的頭結點。ui

上邊的 demo 總結成一張圖以下:this

第一個相似於 HashMap 的結構,利用 Entry 中的 next 指針進行關聯。

下邊則是 LinkedHashMap 如何達到有序的關鍵。

就是利用了頭節點和其他的各個節點之間經過 Entry 中的 afterbefore 指針進行關聯。

其中還有一個 accessOrder 成員變量,默認是 false,默認按照插入順序排序,爲 true 時按照訪問順序排序,也能夠調用:

public LinkedHashMap(int initialCapacity,
                         float loadFactor,
                         boolean accessOrder) {
        super(initialCapacity, loadFactor);
        this.accessOrder = accessOrder;
    }
複製代碼

這個構造方法能夠顯示的傳入 accessOrder

構造方法

LinkedHashMap 的構造方法:

public LinkedHashMap() {
        super();
        accessOrder = false;
    }
複製代碼

其實就是調用的 HashMap 的構造方法:

HashMap 實現:

public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);

        this.loadFactor = loadFactor;
        threshold = initialCapacity;
        //HashMap 只是定義了改方法,具體實現交給了 LinkedHashMap
        init();
    }
複製代碼

能夠看到裏面有一個空的 init(),具體是由 LinkedHashMap 來實現的:

@Override
    void init() {
        header = new Entry<>(-1, null, null, null);
        header.before = header.after = header;
    }
複製代碼

其實也就是對 header 進行了初始化。

put 方法

LinkedHashMapput() 方法以前先看看 HashMapput 方法:

public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                //空實現,交給 LinkedHashMap 本身實現
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        // LinkedHashMap 對其重寫
        addEntry(hash, key, value, i);
        return null;
    }
    
    // LinkedHashMap 對其重寫
    void addEntry(int hash, K key, V value, int bucketIndex) {
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }

        createEntry(hash, key, value, bucketIndex);
    }
    
    // LinkedHashMap 對其重寫
    void createEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<>(hash, key, value, e);
        size++;
    }       
複製代碼

主體的實現都是藉助於 HashMap 來完成的,只是對其中的 recordAccess(), addEntry(), createEntry() 進行了重寫。

LinkedHashMap 的實現:

//就是判斷是不是根據訪問順序排序,若是是則須要將當前這個 Entry 移動到鏈表的末尾
        void recordAccess(HashMap<K,V> m) {
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
            if (lm.accessOrder) {
                lm.modCount++;
                remove();
                addBefore(lm.header);
            }
        }
        
        
    //調用了 HashMap 的實現,並判斷是否須要刪除最少使用的 Entry(默認不刪除) 
    void addEntry(int hash, K key, V value, int bucketIndex) {
        super.addEntry(hash, key, value, bucketIndex);

        // Remove eldest entry if instructed
        Entry<K,V> eldest = header.after;
        if (removeEldestEntry(eldest)) {
            removeEntryForKey(eldest.key);
        }
    }
    
    void createEntry(int hash, K key, V value, int bucketIndex) {
        HashMap.Entry<K,V> old = table[bucketIndex];
        Entry<K,V> e = new Entry<>(hash, key, value, old);
        //就多了這一步,將新增的 Entry 加入到 header 雙向鏈表中
        table[bucketIndex] = e;
        e.addBefore(header);
        size++;
    }
    
        //寫入到雙向鏈表中
        private void addBefore(Entry<K,V> existingEntry) {
            after  = existingEntry;
            before = existingEntry.before;
            before.after = this;
            after.before = this;
        }  
        
複製代碼

get 方法

LinkedHashMap 的 get() 方法也重寫了:

public V get(Object key) {
        Entry<K,V> e = (Entry<K,V>)getEntry(key);
        if (e == null)
            return null;
            
        //多了一個判斷是不是按照訪問順序排序,是則將當前的 Entry 移動到鏈表頭部。 
        e.recordAccess(this);
        return e.value;
    }
    
    void recordAccess(HashMap<K,V> m) {
        LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
        if (lm.accessOrder) {
            lm.modCount++;
            
            //刪除
            remove();
            //添加到頭部
            addBefore(lm.header);
        }
    }
複製代碼

clear() 清空就要比較簡單了:

//只須要把指針都指向本身便可,本來那些 Entry 沒有引用以後就會被 JVM 自動回收。
    public void clear() {
        super.clear();
        header.before = header.after = header;
    }
複製代碼

總結

總的來講 LinkedHashMap 其實就是對 HashMap 進行了拓展,使用了雙向鏈表來保證了順序性。

由於是繼承與 HashMap 的,因此一些 HashMap 存在的問題 LinkedHashMap 也會存在,好比不支持併發等。

號外

最近在總結一些 Java 相關的知識點,感興趣的朋友能夠一塊兒維護。

地址: github.com/crossoverJi…

相關文章
相關標籤/搜索