哈希表和链表实现的Map接口,具有可预测的迭代次序。 这种实现不同于HashMap,它维持于所有条目的运行双向链表。 此链接列表定义迭代排序,通常是将键插入到Map(插入顺序 )中的顺序 。
2.1.2 主要看get方法
public V get(Object key) { Node<K,V> e; //调用HashMap的getNode if ((e = getNode(hash(key), key)) == null) return null; //如果accessOrder为true if (accessOrder) afterNodeAccess(e); return e.value; } //操作元素后改变LinkedHashMap维护的双向链表 void afterNodeAccess(Node<K,V> e) { // move node to last LinkedHashMap.Entry<K,V> last; //如果末尾不是传入的参数 if (accessOrder && (last = tail) != e) { //构建双向循环链表 LinkedHashMap.Entry<K,V> p = (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after; p.after = null; if (b == null) head = a; else b.after = a; if (a != null) a.before = b; else last = b; if (last == null) head = p; else { p.before = last; last.after = p; } //最近使用的将被插入链尾 tail = p; ++modCount; } }每一次增删改查都会操作LinkedHashMap的循环双向链表,将最新修改的节点赋为尾节点,最少使用、最久未使用的节点将变为头节点
2.1.3 LinkedHashMap调用的是HashMap的put方法
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; if ((p = tab[i = (n - 1) & hash]) == null) //这里的newNode是LinkedHashMap的newNode 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调整双向循环链表 afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); //插入后,根据条件操作头节点。 afterNodeInsertion(evict); return null; } Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) { //新建节点 LinkedHashMap.Entry<K,V> p = new LinkedHashMap.Entry<K,V>(hash, key, value, e); //放入链表尾 linkNodeLast(p); return p; } private void linkNodeLast(LinkedHashMap.Entry<K,V> p) { LinkedHashMap.Entry<K,V> last = tail; tail = p; if (last == null) head = p; else { p.before = last; last.after = p; } }LinkedHashMap使用HashMap的数据结构,只是对关键的方法进行重写,如newNode
遍历操作
public static void main(String[] args) throws Exception { LinkedHashMap linkedHashMap = new LinkedHashMap(16, (float) 0.75,true); linkedHashMap.put("1", "1"); linkedHashMap.put("2", "2"); linkedHashMap.put("3", "3"); linkedHashMap.get("1"); Iterator<Map.Entry> iterator= linkedHashMap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry entry = iterator.next(); System.out.println(entry.getKey()+":"+entry.getValue()); } }源码部分
public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> es; //返回LinkedEntrySet对象 return (es = entrySet) == null ? (entrySet = new LinkedEntrySet()) : es; } final class LinkedEntrySet extends AbstractSet<Map.Entry<K,V>> { public final Iterator<Map.Entry<K,V>> iterator() { //返回LinkedEntryIterator对象 return new LinkedEntryIterator(); } } //LinkedEntryIterator继承LinkedHashIterator final class LinkedEntryIterator extends LinkedHashIterator implements Iterator<Map.Entry<K,V>> { public final Map.Entry<K,V> next() { return nextNode(); } } //最终的迭代操作 abstract class LinkedHashIterator { LinkedHashMap.Entry<K,V> next; LinkedHashMap.Entry<K,V> current; int expectedModCount; LinkedHashIterator() { //在构造中 将head赋给next next = head; expectedModCount = modCount; current = null; } public final boolean hasNext() { return next != null; } final LinkedHashMap.Entry<K,V> nextNode() { //直接返回next 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; } }迭代器是从双向循环链表头开始遍历。
//将元素p插入双链表尾部 private void linkNodeLast(LinkedHashMap.Entry<K,V> p) { LinkedHashMap.Entry<K,V> last = tail; tail = p; if (last == null) head = p; else { p.before = last; last.after = p; } } //将src替换为dst private void transferLinks(LinkedHashMap.Entry<K,V> src, LinkedHashMap.Entry<K,V> dst) { LinkedHashMap.Entry<K,V> b = dst.before = src.before; LinkedHashMap.Entry<K,V> a = dst.after = src.after; if (b == null) head = dst; else b.after = dst; if (a == null) tail = dst; else a.before = dst; } //新建节点 Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) { LinkedHashMap.Entry<K,V> p = new LinkedHashMap.Entry<K,V>(hash, key, value, e); linkNodeLast(p); return p; } }总结: LinkedHashMap主体使用HashMap的数据结构,通过重写一些重要的方法如newNode,轻松将HashMap的Entry转换为LinkedHashMap的,体现了继承的优势
LinkedHashMap保持HashMap的数据结构,但是自己也维护一个双向循环链表。
LinkedHashMap内部有accessOrder标记,为false时,双向链表按插入的顺序排列。因为新插入的元素都是尾插法
accessOrder=true时,每次对元素进行增删改查,都会将该元素放到链表尾部。使这个链表将最新操作的元素放入链表尾,长时间未使用的放入头部。这种即是LRU算法。