HashMap源码解读其实很简单
其中里面有几个成员变量
table 代表hashmap数组,它是按照capacity去创建数组大小的,所以大小table.length是capacity。
size 是存放元素的个数。
threshold 构造方法的时候阀值是capacity,put后阀值是capacity*loadFactor
modCount 是操作量
//构造方法(没有去给HashMap设置大小,默认是空集合{})
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;//负载因子(默认0.75)
threshold = initialCapacity;//初始容量就是阈值(默认16)
init();
}
//给HashMap存值
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
//一、判断HashMap为空集合,给集合设置大小
inflateTable(threshold);
}
//二、如果key为null
if (key == null)
return putForNullKey(value);//返回插入前的value
int hash = hash(key);//进行逻辑复杂的hash,为了减少hash冲突
int i = indexFor(hash, table.length);//获取下标,获取放入数组的元素entry
for (Entry<K,V> e = table[i]; e != null; e = e.next) {//遍历链表
Object k;
//判断key完全一样,则覆盖原来的value,返回就得value
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的情况下返回
}
//一、定义HashMap的数组大小(默认16),和阈值(默认12)
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize
int capacity = roundUpToPowerOf2(toSize);
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);//不了解具体用意
}
//number如果大于等于最大容量取最大容量;如果小于最大容量且大于1则是(比当前数减一后小的最大2次幂右移一位),否则就是1
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
return number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
其中方法Integer.highestOneBit((num)的效果如下:
若i为正整数,例如i=9,其二进制为1001,最高位1的权值为8,则返回值为8
二、如果key为null,插入值
private V putForNullKey(V value) {
//数组0号位置是一个链表结构,遍历链表。
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
//如果链表有值且key为null,则覆盖原来值,返回旧值
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;//操作新增数量+1
addEntry(0, null, value, 0);//没有插入过key为null的值,会进行此操作
return null;
}
//插入key为null的值,hash为0,
void addEntry(int hash, K key, V value, int bucketIndex) {
//如果插入的数量大于阀值且当前下标数组不为null,扩充HashMap大小
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);
}
//扩充HashMap大小,2倍的原始容量,并重新放置元素
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
//遍历老的HashMap的元素放置到新的HashMap
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {//遍历旧的HashMap的entry数组
while(null != e) {//遍历旧的HashMap的链表
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
//把旧的HashMap的值放进新的HashMap的链表里
e.next = newTable[i];//1、把新HashMap数组索引下标的链表头entry,赋给新要存进去的entry的next
newTable[i] = e;//2、再把新要存进去的entry设置到链表头
e = next;//遍历旧链表的下一个元素
}
}
}
//新增一个链表元素,size加一
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++;
}