# 构造方法

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



/**
* The iteration ordering method for this linked hash map: <tt>true</tt>
* for access-order, <tt>false</tt> for insertion-order.
*
* @serial
*/
final boolean accessOrder;


## 构造方法

    public LinkedHashMap(int initialCapacity, float loadFactor) {
accessOrder = false;
}

public LinkedHashMap(int initialCapacity) {
super(initialCapacity);
accessOrder = false;
}

super();
accessOrder = false;
}

public LinkedHashMap(Map<? extends K, ? extends V> m) {
super();
accessOrder = false;
putMapEntries(m, false);
}

boolean accessOrder) {
this.accessOrder = accessOrder;
}

# 基本操作

## put()操作

public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//如果桶为空，调用newNode新建一个节点
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//如果找到了节点
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
//子类实现
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
//子类实现
afterNodeInsertion(evict);
return null;
}

### newNode()方法

Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
new LinkedHashMap.Entry<K,V>(hash, key, value, e);
return p;
}
LinkedHashMap.Entry<K,V> last = tail;
tail = p;
if (last == null)
else {
p.before = last;
last.after = p;
}
}

### afterNodeInsertion(boolean evict)实现

void afterNodeInsertion(boolean evict) { // possibly remove eldest
if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}

1. evict为true。只要不是构造方法中的插入Map集合，evict就为true，否则为false
2. first!=null。表明表不为空，按理来说，当调用该方法时，哈希表不会为空
3. removeEldestEntry()方法返回true。该方法删除删除最老的节点

 protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}

public boolean removeEldestEntry(Map.Entry<K,V> eldest){
return size()>100;
}

### afterNodeAccess(Node e)实现

afterNodeAccess()在键值重复时，会调用该方法，其中参数该表示该节点。该方法用于再accessOrder为true时将节点移到最后，其实现如下：

void afterNodeAccess(Node<K,V> e) { // move node to last
//如果accessOrder为true并且当前节点不是tail节点
if (accessOrder && (last = tail) != e) {
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null)
else
b.after = a;
if (a != null)
a.before = b;
else
last = b;
if (last == null)
else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}

1. accessOrder为true。为true只能在三个参数的构造方法中指定accessOrder，表明按照访问顺序管理节点。那么当键相同时，就相当于一次访问，所以可能需要将访问的节点移到双端链表的尾端。
2. 如果当前节点不是尾节点。如果已经是尾节点，那么无须就行移动。

## get()操作

 public V get(Object key) {
Node<K,V> e;
//如果哈希表中不存在该键，返回null
if ((e = getNode(hash(key), key)) == null)
return null;
//如果accessOrder为true，即使用访问顺序
if (accessOrder)
afterNodeAccess(e);
return e.value;
}

1. 调用getNode()方法得到键对应的节点。如果节点为null，表明哈希表中不存在该键，那么返回null
2. 如果哈希表中存在该键并且accessOrder为true，那么调用afterNodeAccess(e)将节点移到双端链表的尾部

## remove()操作

// e表示待删除的节点
void afterNodeRemoval(Node<K,V> e) { // unlink
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.before = p.after = null;
if (b == null)
else
b.after = a;
if (a == null)
tail = b;
else
a.before = b;
}

## keySet()操作

 public Set<K> keySet() {
Set<K> ks;
return (ks = keySet) == null ? (keySet = new LinkedKeySet()) : ks;
}

 final class LinkedKeySet extends AbstractSet<K> {
public final int size()                 { return size; }
public final void clear()               { LinkedHashMap.this.clear(); }
public final Iterator<K> iterator() {
}
public final boolean contains(Object o) { return containsKey(o); }
public final boolean remove(Object key) {
return removeNode(hash(key), key, null, false, true) != null;
}
public final Spliterator<K> spliterator()  {
return Spliterators.spliterator(this, Spliterator.SIZED |
Spliterator.ORDERED |
Spliterator.DISTINCT);
}
public final void forEach(Consumer<? super K> action) {
if (action == null)
throw new NullPointerException();
int mc = modCount;
for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
action.accept(e.key);
if (modCount != mc)
throw new ConcurrentModificationException();
}
}

 final class LinkedKeyIterator extends LinkedHashIterator
implements Iterator<K> {
public final K next() { return nextNode().getKey(); }
}

abstract class LinkedHashIterator {
int expectedModCount;

expectedModCount = modCount;
current = null;
}

public final boolean hasNext() {
return next != null;
}

final LinkedHashMap.Entry<K,V> nextNode() {
LinkedHashMap.Entry<K,V> e = next;
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
current = e;
next = e.after;
return e;
}

public final void remove() {
Node<K,V> p = current;
if (p == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false);
expectedModCount = modCount;
}
}

final class LinkedValueIterator extends LinkedHashIterator
implements Iterator<V> {
public final V next() { return nextNode().value; }
}

implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}

## values()方法

values()方法用于返回哈希表中的所有值，由于哈希表中的键是唯一的，所以keySet()的返回值是一个Set，而哈希表中的值是允许重复的，所以返回值是一个Collection，其实现如下：

 public Collection<V> values() {
Collection<V> vs;
return (vs = values) == null ? (values = new LinkedValues()) : vs;
}

final class LinkedValues extends AbstractCollection<V> {
public final int size()                 { return size; }
public final void clear()               { LinkedHashMap.this.clear(); }
public final Iterator<V> iterator() {
}
public final boolean contains(Object o) { return containsValue(o); }
public final Spliterator<V> spliterator() {
return Spliterators.spliterator(this, Spliterator.SIZED |
Spliterator.ORDERED);
}
public final void forEach(Consumer<? super V> action) {
if (action == null)
throw new NullPointerException();
int mc = modCount;
for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
action.accept(e.value);
if (modCount != mc)
throw new ConcurrentModificationException();
}
}