Java集合包(七)——Map实现类之LinkedHashMap原理

一:特征

  LinkedHashMap的元素迭代是有序的,按照元素的插入顺序。【注意:是插入顺序!】

二:原理

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>
{
    ...
}

  LinkedHashMap继承了HashMap,其数据存储和操作等策略都是复用HashMap的。

  它与HashMap不同地方在于其Entry的定义:

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

  LinkedHashMap的Entry在增加了两个属性:before和after,用来保存该键值对在整合存储结构中的前驱节点和后续节点,从而记住了插入顺序。

原文地址:https://www.cnblogs.com/ygj0930/p/13577660.html