文章目录
1. 预备知识
扩容产生的条件:
- 如果HashMap初始化的时候,没有设置初始容量,那么初始容量默认为16,扩容阈值threshold为16*0.75f=12;
- 如果设置了初始容量,那么首次扩容阈值threshold为初始容量。
HashMap的插入
- JDK1.7的插入是头插法
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;
threshold = initialCapacity;//