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