public V put(K key, V value) {
K k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length); //顺便问问这句,为什么是返回h & (length - 1)呢,这位运算结果的i对应着table里面的第一个元素?不懂 for (Entry<K, V> e = table[i]; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this); //它的具体意思是当put调用的时候,这个方法就会被触发。这句的具体实现找不到?
return oldValue;
}
} modCount++;
addEntry(hash, k, value, i);
return null;
}
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K, V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K, V>(hash, key, value, e); //e是next,怎么感觉table[bucketIndex]的next还是自己,而不是真正的下一个?这个是怎么回事
if (size++ >= threshold)
resize(2 * table.length);
}
hashMap的大小是 length=2^n
hashcode & (length-1) == hashcode % length(模运算)
2.这个函数记录当前的Entry被当前的HashMap修改了
3.Entry是一个单链表,next仍然是指向一个Entry对象.一个串一个,直到某一个entry的next是null为止
是为了计算该hash所应该存放的table索引位置e.recordAccess(this);
是为了子类覆盖提供预留的table[bucketIndex] = new Entry<K, V>(hash, key, value, e);
执行后,原来的table[bucketIndex]已经放入最新的Entry,该Entry.next指向原来存放的Entry
value = v;
next = n;
key = k;
hash = h;
}包含下一个的信息e.recordAccess(this);
貌似还真没有什么用,望知道的解答下