本文对HashMap部分源码按照出现顺序进行翻译解读,绿色英文为HashMap源码中的注释,中文为注释对应翻译,代码段中为相应源码 *仅供参考
Hashput中的put方法是一个重要的方法,那么当put方法执行的过程中,HashMap发生了什么改变呢?
put方法:
Associates the specified value with the specified key in this map.If the map previously contained a mapping for the key, the old value is replaced.
@param key key with which the specified value is to be associated
@param value value to be associated with the specified key
@return the previous value associated with <tt>key</tt>, or
<tt>null</tt> if there was no mapping for <tt>key</tt>.
(A <tt>null</tt> return can also indicate that the map previously associated <tt>null</tt> with <tt>key</tt>.)
将指定的值与此映射中的指定键关联。如果映射之前包含键的映射,则替换旧值。
参数key表示指定值要关联的键,参数value要关联到指定键的值,返回与<tt>键关联的上一个值</tt>,或<tt>null</tt>如果<tt>key</tt>没有映射。
(一个<tt>null</tt>返回值也可以表示先前关联的map <tt>null</tt>与<tt>key</tt>。)
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}