源码基本和hashmap是一样的,只有一个创建节点的方法不同
hashmap的:
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
linkedhashmap的:
void createEntry(int hash, K key, V value, int bucketIndex) {
HashMap.Entry<K,V> old = table[bucketIndex];
Entry<K,V> e = new Entry<>(hash, key, value, old);
table[bucketIndex] = e;
e.addBefore(header);
size++;
}
private void addBefore(Entry<K,V> existingEntry) {
after = existingEntry;
before = existingEntry.before;
before.after = this; //this的值为当前插入的节点对象
after.before = this;
}
会造成 节点顺序: null -> 第一个节点 -> null -> 第二个节点 。。。
不明白的可断点跟踪查看