HashMap概述:
HashMap基于Map接口实现,元素以键值对的方式存储,并且允许使用null 建和null 值, 因为key不允许重复,因此只能有一个键为null,另外HashMap不能保证放入元素的顺序,它是无序的,和放入的顺序并不能相同。
HashMap的容量,默认是16
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
HashMap的加载因子,默认是0.75
/**
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
当HashMap中元素数超过容量*加载因子时,HashMap会进行扩容。
实现原理::
HashMap本质是一个一定长度的数组,数组中存放的是链表。
HashMap类中的元素是Node类,翻译过来就是节点,是定义在HashMap中的一个内部类,实现了Map.Entry接口。
Node类定义的源码:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
Node类的基本属性有:
hash:key的哈希值
key:节点的key,类型和定义HashMap时的key相同
value:节点的value,类型和定义HashMap时的value相同
next:该节点的下一节点
所以Node可以变成链表,但不是双向链表,里面有一个hash的int值,这个值是现算得(高位取反,计算后去低位异或),它和key有关,但是不是key的hashcode值
拉链法
由Node节点组成链表之后,HashMap定义了一个Node数组:table
transient Node<K,V>[] table;
扩容机制
我们通过put方法来看往一个空的HashMap里放一个值得时候会发生什么。
1.当用户调用put(k,v)方法,对象执行该方法。
我们进去put方法的源码
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key //刚才算的值
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value //如果存在,则不改变它的值
* @param evict if false, the table is in creation mode. //如果不存在,去放值
* @return previous value, or null if none
*/
2.对象执行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;
if ((tab = table) == null || (n = tab.length) == 0)// 由于是第一次放值,所以table一定为空
n = (tab = resize()).length;//resize方法初始化table
进入到resize方法(截取第一次用到代码)
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;//16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//12
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//创建一个长度为16的数组
table = newTab;
...
return newTab;
table初始化完成后,putval依次放值
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null) //注释1
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k)))) //注释2
e = p;
else if (p instanceof TreeNode) //注释3
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash); //注释4
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold) //注释5
resize();
afterNodeInsertion(evict);
return null;
}
}
}
代码解析:
1,注释1,table对应位置无节点,则创建新的Node节点放入对应位置。
2,注释2,table对应位置有节点,如果hash值匹配,则替换。
3,注释3,table对应位置有节点,如果table对应位置已经是一个TreeNode,不再是Node,也就说,table对应位置是TreeNode,表示已经从链表转换成了红黑树,则执行插入红黑树节点的逻辑。
4,注释4,table对应位置有节点,且节点是Node(链表状态,不是红黑树),链表中节点数量大于TREEIFY_THRESHOLD,则考虑变为红黑树。实际上不一定真的立刻就变,table短的时候扩容一下也能解决问题,后面的代码会提到。
5,注释5,HashMap中节点个数大于threshold,会进行扩容
第二次扩容
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1;
长度扩容到32,加载因子为24
然后将老的数组赋值到新数组中
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
第二次扩容完成
扩容总结
首先创建HashMap的时候,底层数组还没有初始化,它会去检查当前的table,如果table为空,会去拿到默认的大小(16)和加载因子(12),创建长度为16的数组。以Node为节点,put方法的key转为hash(高反低异),节点的hash值和当前长度-1进行与运算,也就是对16取模,得到当前值应该被防止的位置。如果这个位置已经由值存在,则被放置到已经存在的值之后(链表)。
当一直存放到第13个值得时候(第12个值不扩容),调用resize方法 长度扩容到32,加载因子为24 。
原来的值可能形成链表,链表之前的值是对16取模,比如在3这个节点上,现在要对32取模,那么就有可能在两个节点上,要么在3上,要么在19上 所以求需要将链表上的值都拿出来,来确定是在3这个节点上还是19节点上,判断完成后,有一个lohead(低位)和hihead(高位),两个都形成了一个新的链表,于是将lohead的值保存到原来的位置上, hihead的值将被放到19的位置上