1.HashMap底层原理:
(1)数据结构:数组+链表+红黑树。
主数组初始化长度为16,以key-value的方式存储值,其中key唯一且可以为null,为null则存储在数组索引为0的位置。
(2)map.put(k,v)实现原理:
当执行put方法时,先通过hash算法计算key的hash值,判断数组是否为null,空则创建。然后通过公式(length-1)&hash计算Node<K,V>对象在数组中的索引。
如果该索引无元素,直接添加;
如果该索引位置有元素且has值和key均相同,直接替换;否则将其转化为链表,以链表的形式存储值。当链表的数量大于8,将其转化为红黑树。
哈希冲突:哈希算法计算出来的hash值相同。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
// hashcode 无符号位移 16 位
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
// tab 为空则创建
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 计算 index,并对 null 做处理
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;
}
(3)gut方法实现原理:
对 key的hashCode() 做hash计算,然后根据hash值再计算数组的index
如果桶中的第一个节点命中,直接返回;
如果有冲突,则通过 key.equals(k) 去查找对应的entry
若为树,则在红黑树中通过 key.equals(k) 查找,O(logn);
若为链表,则在链表中通过 key.equals(k) 查找,O(n)。
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
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) {
// 在树中 get
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 在链表中 get
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
(4)计算哈希值
(5)扩容
和扩容相关的参数:
初始容量:static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
加载因子:static final float DEFAULT_LOAD_FACTOR = 0.75f;
阈值:int threshold; 阈值=容量*加载因子。默认12。
当元素数量超过阈值时便会触发扩容,每次扩容是原来容量的2倍。
注意:
1.HashMap的容量是有上限的,必须小于1<<30,即1073741824。如果容量超出了这个数,则不再增长,且阈值会被设置为Integer.MAX_VALUE( ,即永远不会超出阈值了)。
2.扩容是2倍的原因:如果不是2倍将增加哈希碰撞的概率
3.加载因子是0.75的原因:如果装填因子是1, 那么数组满了再扩容,可以做到 最大的空间利用率 但是这是一个理想状态,元素不可能完全的均匀分布,很可能就哈西碰撞产生链表了。产生链表的话 查询时间就长了。
---》空间好,时间不好。
那么有人说 ,把装填因子搞小一点,0.5, 如果是0.5的话,就浪费空间,但是可以做到 到0.5就扩容 ,然后哈西碰撞就少,不产生链表的话,那么查询效率很高
---》时间好,空间不好==