LinkedHashMap是HashMap的子類,他們最大的不一樣是,HashMap內部維護的是一個單向的鏈表數組,而LinkedHashMap內部維護的是一個雙向的鏈表數組。HashMap是無序的,LinkedHashMap能夠根據訪問順序或者插入順序進行排序(默認是根據插入順序的,當設置accessOrder爲true時會按照訪問順序排序),當按照訪問順序排序的時候,每次get或put操做時,都會將該節點放到鏈表的末尾html
LinkedHashMap是HashMap的子類,它的內部一樣是使用了hashMap定義的鏈表數組存儲的數據。只不過在HashMap中使用的是單想鏈表Node<k,v>做爲每一個節點,而LinkedHashMap使用了集成了HashMap.Node<k,v>的雙向鏈表Entry<k,v>來做爲每一個節點,Entry<k,v>在Node<k,v>的基礎上增長了Entry<K,V> before, after兩個屬性,將數組中的先後元素關聯起來,這樣的實現也爲LinkedHashMap根據插入順序或訪問順序提供了可能數組
LinkedHashMap是HashMap的子類,它提供了五種構造方法,緩存
public LinkedHashMap(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); accessOrder = false; } public LinkedHashMap(int initialCapacity) { super(initialCapacity); accessOrder = false; } public LinkedHashMap() { super(); accessOrder = false; } public LinkedHashMap(Map<? extends K, ? extends V> m) { super(); accessOrder = false; putMapEntries(m, false); } public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) { super(initialCapacity, loadFactor); this.accessOrder = accessOrder; }
從以上咱們能夠看出,前面四中構造方法都是默認的accessOrder=false,即按找插入順序排序,第五種構造方法咱們能夠本身定義accessOrder的值,當爲true時,說明是按照訪問順序排序的,每次put或get操做的時候都會將元素移到鏈表的末尾數據結構
因爲LinkedHashMap繼承了HashMap類,並且並無重寫put,get等方法,因此在這裏對於LinkedHashMap的基礎操做的源碼就再也不分析,感興趣的能夠參考上一篇關於HashMap的學習筆記。在這裏重點看一下LinkedHashMap提供的removeEldestEntry(Map.Entry<K,V> eldest)方法ide
/** * Returns <tt>true</tt> if this map should remove its eldest entry. * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after * inserting a new entry into the map. It provides the implementor * with the opportunity to remove the eldest entry each time a new one * is added. This is useful if the map represents a cache: it allows * the map to reduce memory consumption by deleting stale entries. * * <p>Sample use: this override will allow the map to grow up to 100 * entries and then delete the eldest entry each time a new entry is * added, maintaining a steady state of 100 entries. * <pre> * private static final int MAX_ENTRIES = 100; * * protected boolean removeEldestEntry(Map.Entry eldest) { * return size() > MAX_ENTRIES; * } * </pre> * * <p>This method typically does not modify the map in any way, * instead allowing the map to modify itself as directed by its * return value. It <i>is</i> permitted for this method to modify * the map directly, but if it does so, it <i>must</i> return * <tt>false</tt> (indicating that the map should not attempt any * further modification). The effects of returning <tt>true</tt> * after modifying the map from within this method are unspecified. * * <p>This implementation merely returns <tt>false</tt> (so that this * map acts like a normal map - the eldest element is never removed). * * @param eldest The least recently inserted entry in the map, or if * this is an access-ordered map, the least recently accessed * entry. This is the entry that will be removed it this * method returns <tt>true</tt>. If the map was empty prior * to the <tt>put</tt> or <tt>putAll</tt> invocation resulting * in this invocation, this will be the entry that was just * inserted; in other words, if the map contains a single * entry, the eldest entry is also the newest. * @return <tt>true</tt> if the eldest entry should be removed * from the map; <tt>false</tt> if it should be retained. */ protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return false; }
這個方法就是判斷是否須要刪除LinkedHashMap中最舊的元素,默認是false的。當咱們須要利用hashmap作一些數據的緩存,可能有的數據是使用了一兩次以後就失去了價值,此外咱們也不想看到緩存的map無限的增加又不想本身手動的去維護這個map。此時咱們就能夠使用LinkedHashMap來完成這個功能;學習
public class Demo { static class MyMap<k,v> extends LinkedHashMap<k,v>{ //定義一個繼承LinkedHashMap的類,並重寫removeEldestEntry方法 int size; public MyMap (int size){ //只提供一個有參的構造方法,用來設置map的大小 /* * 這裏調用的是LinkedHashMap的LinkedHashMap(int initialCapacity,boolean accessOrder) * 構造方法,設置按訪問順序排序 */ super(16,0.75f,true); this.size = size; } @Override protected boolean removeEldestEntry(Entry<k, v> eldest) { // TODO Auto-generated method stub return this.size<size(); //設置當map裏面存放的元素超過MyMap設置的size時就刪除最老的元素 } } public static void main(String[] args) { Map<String,String> map = new MyMap<String,String>(5); for (int i = 0; i < 10; i++) { map.put(i+"", i+""); System.out.print(i+"="+map.size()+"\t"); } System.out.println(); //遍歷map看看裏面的元素 map.forEach((k,v)->{ System.out.println("k="+k); }); //此時訪問一下里面的元素,再觀察遍歷後的順序 map.get("6");map.get("8"); System.out.println("========================"); map.forEach((k,v)->{ System.out.println("k="+k); }); } }
打印的結果:
0=1 1=2 2=3 3=4 4=5 5=5 6=5 7=5 8=5 9=5
k=5
k=6
k=7
k=8
k=9
========================
k=5
k=7
k=9
k=6
k=8this
在學完LinkedHashMap和HashMap以後咱們會發現,這兩種集合類是很是類似的,兩者都是經過內部數組去保存數據的,不一樣的是HashMap數組中的每一個元素都是一個單向的鏈表,指向了產生hash衝突時和它自己具備相同hash值的元素,因此HashMap只能是無序的。LinkedHashMap中數組裏面的每個元素都是一個雙向鏈表,它不只指向了產生hash衝突時下一個跟它自己具備相同hash值的位置,還指定了它的上一個和下一個元素(能夠是插入順序的上一個下一個,也能夠試訪問順序的上一個下一個),這樣爲LinkedHashMap實現有序提供了可能spa