
眾所周知 HashMap 是一個無序的 Map,因為每次根據 key 的 hashcode 映射到 Entry 數組上,所以遍歷出來的順序並不是寫入的順序。
因此 JDK 推出一個基於 HashMap 但具有順序的 LinkedHashMap 來解決有排序需求的場景。
它的底層是繼承於 HashMap 實現的,由一個雙向鏈表所構成。
LinkedHashMap 的排序方式有兩種:
- 根據寫入順序排序。
- 根據訪問順序排序。
其中根據訪問順序排序時,每次 get 都會將訪問的值移動到鏈表末尾,這樣重復操作就能的到一個按照訪問順序排序的鏈表。
數據結構
@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 繼承於 HashMap 的 Entry,並新增了上下節點的指針,也就形成了雙向鏈表。
還有一個 header 的成員變量,是這個雙向鏈表的頭結點。
上邊的 demo 總結成一張圖如下:

第一個類似於 HashMap 的結構,利用 Entry 中的 next 指針進行關聯。
下邊則是 LinkedHashMap 如何達到有序的關鍵。
就是利用了頭節點和其余的各個節點之間通過 Entry 中的 after 和 before 指針進行關聯。
其中還有一個 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 方法
看 LinkedHashMap 的 put() 方法之前先看看 HashMap 的 put 方法:
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 相關的知識點,感興趣的朋友可以一起維護。
