HashMap底层原理是什么?
HashMap在产生哈希冲突的时候,是用的头插法还是尾差法?
哈希冲突有哪几种解决冲突的办法?
HashMap是一个用来存储Key-Value键值对的集合,每一个键值对也叫Enery。
这些键值对(Entry)分散存储在一个数组当中,这个数组就是HashMap的主干。
HashMap数组的每一个元素的初始值都是Null。
当调用put方法的时候,这个时候我们需要利用一个哈希函数来确定Entry的插入位置index:
看了很多文章,发现每篇文章说的都不一样,所以还是分版本分析
jdk1.6
//put方法源码
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
//通过key的hashCode来计算Hash值,最后在通过Indexfor方法得到插入位置
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
//如果发现key已经在链表中存在,则修改并返回旧的值
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
//如果遍历链表没发现这个key,则会调用以下代码
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[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
//键值对的构造方法
Entry( int h, K k, V v, Entry<K,V> n) {
value = v;
//让新键值对指向原键值对(这个地方很明显就是头插法了,原因据说是HashMap的发明者说后插入的查询的概率大)
next = n;
key = k;
hash = h;
}
从上面的put方法可以看出,数组是存了一个Entry,如果发现了hash冲突的情况下,是用链表来存储的
但是这个时候应该也发现了,如果有很大的一系列键值对的哈希值是一样的,那么他们就会都存在同一个链表中,那么这个时候
就会出现和链表一样的问题,查询很慢了,所以jdk1.8就出来了,引入了红黑树来解决
//jdk1.8中是怎么做的呢
//put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param 哈希值
* @param key
* @param value
* @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
*/
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) {
//e是p的下一个结点
if ((e = p.next) == null) {
//插入链表的尾部,可以看到最后的指向的结点的node引用为Null,说明是在链表尾部
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//如果插入后链表长度大于8则转化为红黑树
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;
}
HashMap.Node<K, V> newNode(int var1, K var2, V var3, HashMap.Node<K, V> var4) {
return new HashMap.Node(var1, var2, var3, var4);
}
Node(int var1, K var2, V var3, HashMap.Node<K, V> var4) {
this.hash = var1;
this.key = var2;
this.value = var3;
this.next = var4;
}
//为什么头插改尾插,其实也是为了避免在多线程扩容情况下,出现环形链表的情况,导致下次查询的时候就出现了死循环