前言
首先我们来回顾一下HashMap的一些特点。
1.Node<K,V>是HashMap元素存储的最小单元。
2.Node<K,V>的集合是数组,即Node<K,V>[]。
3.HashMap是线程不安全的。
4.HashMap的迭代的顺序和存储的顺序不一致,即取的顺序和存的顺序不一致的现象。
5.HashMap的键和值都允许为空。
假设我们有一个需要,要求元素的取出来的顺序和存进去的顺序完全一致。那么HashMap已经不能满足,此时,就需要用到HashMap的子类LinkedHashMap。
LinkedHashMap的基本结构
1 public class LinkedHashMap<K,V> 2 extends HashMap<K,V> 3 implements Map<K,V>
LinkedHashMap是HashMap的子类。因此 LinkedHashMap具有HashMap公有的方法。
我们再来看一下LinkedHashMap的最小存储单元,
1 /** 2 * HashMap.Node subclass for normal LinkedHashMap entries. 3 */ 4 static class Entry<K,V> extends HashMap.Node<K,V> { 5 Entry<K,V> before, after; 6 Entry(int hash, K key, V value, Node<K,V> next) { 7 super(hash, key, value, next); 8 } 9 }
Entry<K,V>继承自HashMap中的内部类HashMap.Node<K,V>,同时增加了两个成员属性,before和after,也就是前驱结点和后继结点。
我们再看看一下LinkedHashMap的基本结构。
未完待续....
结束语