voidcreateEntry(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++; }
在之前的基础上还维护了一种链表结构。将新添加的数据放在链表最后面
1 2 3 4 5 6
privatevoidaddBefore(Entry<K,V> existingEntry){ after = existingEntry; before = existingEntry.before; before.after = this; after.before = this; }
这个方法就是将当期元素放在列表的最后面。
1 2 3 4 5 6 7
public V get(Object key){ Entry<K,V> e = (Entry<K,V>)getEntry(key); if (e == null) returnnull; e.recordAccess(this); return e.value; }