LoadFactor
负载因子,默认为0.75
threshold
所能容纳键值对的临界值,计算公式为数组长度*负载因子
size
hashMap中实际的键值对数量
modCount
用来记录hashmap内部结构发生变化的次数
hashMap的默认容量INITIAL_CAPACITY为16
TREEIFY_THRESHOLD
树化阈值(超过8之后,链表升级为树)
UNTREEIFY_THRESHOLD
存储结构
HashMap采用了数组+链表+红黑树的存储结构
数组部分称为哈希桶,当链表长度大于8时将采用红黑树进行存储,当长度降到6时,转成链表
链表的时间复杂度为o(n)
红黑树的时间复杂度为o(logn)
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;
}
}
Node实现了Map.Entry 接口,本质是一个键值对
数据插入
当向hashMap中插入数据时,首先要确定在hash桶中的具体位置。那么如何确定node的存储位置呢
-
获取key的hash值
-
经过hash值扰动函数,使得hash值更散列,
hashmap还没扩容很大的时候,length很小,而 index = (length-1)& hashcode,让hash值的高位也参与寻址计算
-
构造node对象
-
路由算法,找出node应存放的位置(路由寻址公式:(table.length-1)&node.hash)
Hash碰撞
hash值相同时,put放入对应链表中。一直碰撞,查找低效。
什么是链化
红黑树解决链化问题,提高查找效率
扩容原理
原因:如果hash表元素很多,get的时候,理想情况是o(1),链化严重的话,变成o(n)了,扩容就是使数组变大,使元素分散。
为了解决hash冲突,导致链化严重,为了提高查找性能,空间换时间,
1.简述put方法:
-
首先检查数组是否为空,为空的时候需要调用扩容方法,初始化一个长度为16,阈值为8的数组
-
计算key’的hash值,hash值&(桶长度-1)找到应该在桶中存储的位置,
-
如果桶中这个位置的元素为空,则直接赋值
-
如果不为空,则比较key是否相等,相等的话满足条件覆盖
-
如果key值不相等,比较类型为红黑树,则调用红黑树的插入,类型为链表,则采用尾插法插入。
-
插入完成后,查看map的size是否大于阈值,大于阈值,则要扩容
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)
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))))
e = p;
else if (p instanceof TreeNode)
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);
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)
resize();
afterNodeInsertion(evict);
return null;
}
简述resize方法
-
如果数组为空,则新建一个默认长度为16,阈值为8的数组并返回
-
如果数组不为空,说明里面有数据,首先检查数组长度是否大于最大长度,如果超过,则将阈值设为整形最大值,表示不再扩容
-
否则将数组长度2,阈值大小 *2 建立一个新table。
-
然后需要将原来table的数据,转移到新的table中。遍历oldTable 如果里面的首元素不为空,如果该元素指向的下一个元素为空, 根据路由算法,计算该元素在新数组中的位置index,直接赋值给新table
-
如果该元素指向的下一个元素不为空,并且该元素类型为链表,则遍历该链表,这里位置可能是index,也有可能是index+oldLength.
这里有一个简易算法,直接hashcode&newLength,为0则采用尾插法,插入低位链index,为1,则采用尾插法插入高位链index+oldLength
6.类型为红黑树,会把数据先拿出来放到新表中,如果新表会不会有红黑树取决于数据放进去之后,而不是把整个红黑树移动过去;其实和链表也差不多,它也会先分成两个链,高位链和低位链,然后把链放到新表中;
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 (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
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;
}