首先第一步:
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
第一个方法中首先判断非空,如果是空则初始化,不是重点不讲解。
接下来key是否为null的判定。如果是则出现第二个方法:
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
我们假设你的key==null则第二个方法中的执行逻辑为
遍历table并判断其中是否已经存在一个entry的key==null。如果已经存在则返回旧值并用新值替换,这没啥说的。
接下来如果没有,则调用第三个方法:
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
第三个方法中我们看到首先根据threshold和table中你所给出的桶索引位置是否为null来判断是否需要扩展map。不是要说的重点。
接下来不管扩不扩容,第四个方法才是真正意义上的插入方法:
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++;
}
第四个方法中我们看到了,首先根据你给出的桶索引去到对应的entry e;
然后执行赋值:table[bucketIndex] = new Entry<>(hash, key, value, e);
那么这句就是把新的已经携带了你要添加的key和value值的entry添加到map中。
那么这句的关键就是第五个方法也就是new Entry<>的构建了:
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
根据Entry的构造函数可以看出,把最后一个参数赋值给了next,而当前的k和value就是当前值。而n值就是原有的entry的完整值。
所以可以得出是添加到头部,而不是结尾处。
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
第一个方法中首先判断非空,如果是空则初始化,不是重点不讲解。
接下来key是否为null的判定。如果是则出现第二个方法:
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
我们假设你的key==null则第二个方法中的执行逻辑为
遍历table并判断其中是否已经存在一个entry的key==null。如果已经存在则返回旧值并用新值替换,这没啥说的。
接下来如果没有,则调用第三个方法:
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
第三个方法中我们看到首先根据threshold和table中你所给出的桶索引位置是否为null来判断是否需要扩展map。不是要说的重点。
接下来不管扩不扩容,第四个方法才是真正意义上的插入方法:
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++;
}
第四个方法中我们看到了,首先根据你给出的桶索引去到对应的entry e;
然后执行赋值:table[bucketIndex] = new Entry<>(hash, key, value, e);
那么这句就是把新的已经携带了你要添加的key和value值的entry添加到map中。
那么这句的关键就是第五个方法也就是new Entry<>的构建了:
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
根据Entry的构造函数可以看出,把最后一个参数赋值给了next,而当前的k和value就是当前值。而n值就是原有的entry的完整值。
所以可以得出是添加到头部,而不是结尾处。