HashMap extends AbstractHashMap,实现remove, containsKey, put, get等方法。
HashMap的底层是数组和链表
static class Nodeimplements Map.Entry { final int hash; final K key; V value; Node next; Node(int hash, K key, V value, Node next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); }
Node的实现依赖于Entry
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;
}
Entry
interface Entry{ K getKey(); V getValue(); V setValue(V value); boolean equals(Object o); int hashCode(); public static , V> Comparator > comparingByKey() { return (Comparator > & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); }
由于会发生哈希冲突所以有了链表的存在。
由于Entry的Key值有可能是相同值,所以此时就发生了哈希冲突,key值相同所以会摆放在同一个key的位置,向下组成链表。如图:
位运算的速度更快,公式为:2^n (n-1) res,所以定是二的次方,当需要扩容的时候,容量变为原来的2倍,初始容量为16,负载因子即阈值设为0.75,即entry的key实际可以利用的只有12,当到达12时就会resize或者transfer,减少并发情况的发生。
若阈值变大 ,同空间可以存储更多的数组,但是哈希冲突的概率会变大,
若阈值变小,数组的利用率降低
昨天看了些文档,今天想整理复习一下,发现讲不明白,还有拉链法,红黑树将冲突的链表排成红黑树这样查找得更快,数组成环情况还未搞明白。



