概述
带一层环形双链表的HashMap,非线程安全。
1)在HashMap中迭代器的遍历效率不高,该双链表层就是为了方便键值对的全遍历;
2)在增删改查操作中维护该双链表,所以其性能较HashMap稍低;
3)可以实现简单的LRU缓存功能;
4)迭代器fail-fast;
数据结构
在HashMap基础上增加一层双链表,其与HashMap层的数组、单链表数据结构没有任何关系:
private transient Entry<K,V> header; // 双链表层的标记节点
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);
}
// 在双链表层删除该节点
private void remove() {
before.after = after;
after.before = before;
}
// 在双链表层,在指定的existingEntry节点前链接该节点
private void addBefore(Entry<K,V> existingEntry) {
after = existingEn