## 1.概述

``````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);

}

}``````

``````@Test

map.put(1, null);

map.put(2, null);

map.put(3, null);

map.put(4, null);

map.put(5, null);

Set<Integer> keys = map.keySet();

Integer[] arr = keys.toArray(new Integer[0]);

for (int i = 0; i < arr.length; i++) {

assertEquals(new Integer(i + 1), arr[i]);

}

}``````

``LinkedHashMap<Integer, String> map = new LinkedHashMap<>(16, .75f, true);``

``````@Test

map.put(1, null);

map.put(2, null);

map.put(3, null);

map.put(4, null);

map.put(5, null);

Set<Integer> keys = map.keySet();

assertEquals("[1, 2, 3, 4, 5]", keys.toString());

map.get(4);

assertEquals("[1, 2, 3, 5, 4]", keys.toString());

map.get(1);

assertEquals("[2, 3, 5, 4, 1]", keys.toString());

map.get(3);

assertEquals("[2, 5, 4, 1, 3]", keys.toString());

}``````

``````public class MyLinkedHashMap<K, V> extends LinkedHashMap<K, V> {

private static final int MAX_ENTRIES = 5;

int initialCapacity, float loadFactor, boolean accessOrder) {

}

@Override

protected boolean removeEldestEntry(Map.Entry eldest) {

return size() > MAX_ENTRIES;

}

}``````

``````@Test

map.put(1, null);

map.put(2, null);

map.put(3, null);

map.put(4, null);

map.put(5, null);

Set<Integer> keys = map.keySet();

assertEquals("[1, 2, 3, 4, 5]", keys.toString());

map.put(6, null);

assertEquals("[2, 3, 4, 5, 6]", keys.toString());

map.put(7, null);

assertEquals("[3, 4, 5, 6, 7]", keys.toString());

map.put(8, null);

assertEquals("[4, 5, 6, 7, 8]", keys.toString());

}``````

## 6.並發

``Map m = Collections.synchronizedMap(new LinkedHashMap());``

HashMap的區別在於需要進行結構修改的地方。在訪問順序鏈接的哈希表中，僅調用get API會導致結構上的修改。除此之外，還有諸如putremove之類的操作。