传送门
深入理解HashMap(一)hashmap所用算法、构造函数
深入理解HashMap(二)put方法解析
深入理解HashMap(三)resize方法解析
深入理解HashMap(四)get方法解析
深入理解HashMap(五)remove方法解析
put方法是hashmap的核心方法之一,我们来看一下put方法的源码长什么样:
put方法中调用了putVal方法,putVal方法中有一个参数为hash(key),先看一下hash(key)是怎么计算的:
这里的算法上一篇有讲,hash值的算法,可以回去看一下~
进去putVal方法看一下:
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果table没有初始化,则初始化table,并且返回长度
if ((tab = table) == null || (n = tab