HashMap底层实现原理是什么?A8站源码交易平台
HashMap由数组+链表组成,JDK8中新增了红黑树,当链表长度到达8(默许阈值)时,链表转化成红黑树,链表过长对功能有很大的影响。
//HashMap初始化长度
static final int DEFAULT_INITIAL_CAPACITY = 1<<4;//位运算,1左移四位是16
//HashMap最大长度
static final int MAXIMUM_CAPACITY = 1<<30;//1073741824
//默许加载因子(扩容因子)
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//链表转化成红黑树的阈值
static final int TREEIFY_THRESHOLD = 8;
//红黑树转化成链表的阈值
static final int UNTREEIFY_THRESHOLD = 6;
//最小树容量
static final int MIN_TREEIFY_CAPACITY = 64;
PS:HashMap构造时可以指定默许初始巨细和负载因子,
经典面试题:
1.JDK8中HashMap扩容时做了哪些优化?
JDK8在hashmap扩容时不再从头核算每个元素的哈希值,而是经过高位运算(e.hash & oldCap)来判定元素是否需求移动。
例如:
key1.hash = 10 0000 1010;
key2.hash = 10 0001 0001;
oldCap = 16 0001 0000;(oldCap就是扩容前table.length)
key1.hash & oldCap的高一位为0,扩容时元素下标不变;
key1.hash & oldCap的高一位为1,扩容时元素下标=原下标+原数组长度。
PS:与运算,有0则0,全1则1。
且JDK8新增元素选用的是尾插法(尾部正序刺进),而JDK7是头部刺进(头部倒序刺进),JDK8有用的避免了JDK7在扩容时的死循环和数据丢掉的问题,但是依然存在数据掩盖的问题,这也就是HashMap线程不安全的一部分原因。
2.加载因子为什么是0.75?
加载因子是来判别什么时分进行扩容。
当加载因子设置比较大时,扩容发作的频率比较低且占用的空间会比较小,但这样的话发作hash抵触的几率就会增大,因而需求更杂乱的数据结构去存储数据,这样对元素的操作时刻添加,运行效率会降低;
当加载因子设置较小的时分,会发作频频的扩容且占用空间增大,此刻hash抵触的可能性就比较小,操作功能会提高
3.当有哈希抵触时,HashMap是怎么查找并承认元素的?
当哈希抵触时需求经过判别 key 值是否持平,才能承认此元素是不是咱们想要的元素。
4.HashMap源码中有哪些重要的方法?A8站源码交易平台
查询、新增和数据扩容
查询:查询时先比较key的hashcode然后去比较key值,将对应的value值回来;
public V get(Object key) {
Node
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node
Node
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//判别第一个节点是否是需求的值
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//假如下一个节点不为空
if ((e = first.next) != null) {
//判别是否是红黑树结构-是的话走树的查找方法
if (first instanceof TreeNode)
return ((TreeNode
//循环,hash持平且key持平
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
新增:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node
//假如链表为空则创建链表
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//根据key的hash值核算要刺进的数组索引i,假如tab[i] == null,则直接刺进
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node
//假如key已存在,掩盖原值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
//红黑树刺进树节点
e = ((TreeNode
else {
//链表结构训话刺进,放在链表的尾部(JDK7是刺进链表头部)
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1)
//判别转化成红黑树还是扩容
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;
}
final void treeifyBin(Node
int n, index; Node
//当整个map中元素个数小于64时,只是进行扩容
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode
do {
TreeNode
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
数据扩容:
final Node
Node
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;//扩容前的阈值
int newCap, newThr = 0;
if (oldCap > 0) {
//假如已经到达最大容量,只将阈值设置成Integer.MAX_VALUES,回来
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//假如扩容后newCap未到达最大容量且oldCap大于初始巨细16
//将阈值变为本来的两倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1;
}
//假如oldCap大于代表初始化时用的是HashMap的有参构造
else if (oldThr > 0)
newCap = oldThr;
else {//HashMap的默许构造
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
table = newTab;
//判别原数据不为空,开端将数据转移到新table里边
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)//假如只要一个链表
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
//红黑树操作
((TreeNode
else {
//链表复制,JDK8扩容优化
Node
Node
Node
do {
next = e.next;
//&运算,假如高一位为0,坚持原索引
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
//不然,新索引=原索引+oldCap
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;
}
5.Hashmap是怎么导致死循环的?
JDK7为例
线程1->put(key(3))
线程2->put(key(7))
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry
while(null != e) {
Entry
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
当线程1履行到”Entry
6.为什么HashMap线程不安全?
https://blog.csdn.net/swpu_ocean/article/details/88917958
小结:
HashMap并发的情况下自身就不是线程安全的,主张使用ConcurrentHashMap
A8站源码交易平台