LinkedHashMap解析

LinkedHashMap比较简单,因为大部分内容与HashMap相同。

  • LinkedHashMap 是 HashMap 的子类。
  • 在HashMap存储结构的基础上,使用了一对双向链表来记录添加元素的顺序。
  • 与LinkedHashSet类似,LinkedHashMap 可以维护 Map 的迭代 顺序:迭代顺序与 Key-Value 对的插入顺序一致。
     

HashMap中的内部类:Node

static class Node<K,V> implements Map.Entry<K,V> { 
     
     final int hash; 
     final K key; 
     V value; 
     Node<K,V> next; 
}

 LinkedHashMap中的内部类:Entry

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); 
             } 
}
发布了79 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sinat_30955745/article/details/105729251