一、hashmp参数介绍
static final int DEFAULT_INITIAL_CAPACITY = 16; //默认初始化集合大小
static final int MAXIMUM_CAPACITY = 1 << 30;//设定集合的最大值 相当于2的30次方
static final float DEFAULT_LOAD_FACTOR = 0.75f; // 扩展因子,当集合已经被使用到0.75时对集合扩展
二、方法介绍
//hashmap构造方法
public HashMap(int initialCapacity, float loadFactor) {
//初始化容量不能小于0
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);
// Find a power of 2 >= initialCapacity
//默认从1开始初始化集合,当小于构造方法传来的集合容量大小时,进行移位操作,找到刚好大于初始化的容量的值,一般都为2的N次方
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
//算出集合下一次需要初始化的大小
threshold = (int)(capacity * loadFactor);
//构建出一个大小为capacity数组表
table = new Entry[capacity];
//程序预留方法,方便后续扩展,这个操作在spring继承封装serlvet哪里体现的淋漓尽致
init();
}
get方法
public V get(Object key) {
if (key == null)
return getForNullKey();
//根据传来的key,计算hashcode值
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
//当hash值一样,且对象一样时,返回他的值, 这个判断提现hash自反性
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
//将补充哈希函数应用于给定的哈希代码防范低质量的哈希函数。这是至关重要的因为
HashMap使用两倍长度的哈希表否则,将遇到没有区别的散列码的冲突在较低的部分。注意:Null键总是映射到hash 0,因此索引0
static int hash(int h) {
这个函数确保哈希代码的不同每个位位置的常数倍数都有一个界碰撞次数(默认负载系数为8次)。
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
//返回哈希码h的索引
static int indexFor(int h, int length) {
return h & (length-1);
}
put方法
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
//计算key值的hashcode
int hash = hash(key.hashCode());
//计算出在table的索引位置
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
//解决hash冲突 用久的值替换新的值
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
//记录hashmap数据结构被改变的次数
modCount++;
addEntry(hash, key, value, i);
return null;
}
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
//重新排序这个table
table[bucketIndex] = new Entry<>(hash, key, value, e);
//本次添加后的值如果大于扩容值,进行2倍数扩容
if (size++ >= threshold)
resize(2 * table.length);
}