1、核心属性
//最大容量 2的30次方
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//哈希桶,存放链表。 长度是2的N次方,或者初始化时为0.
transient Node<K,V>[] table;
//加载因子,用于计算哈希表元素数量的阈值。
final float loadFactor;
/* 哈希表内元素数量的阈值,当哈希表内元素数量超过阈值时,会发生扩容resize()。
* threshold = capacity * loadFactor;(容量*加载因子)
*/
int threshold;
2、构造方法
public HashMap() {
//使用默认加载因子0.75f
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
3、核心方法
3.1、put()操作
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* onlyIfAbsent:如果是true,则不修改存在的值
*/
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)
//如果当前的哈希表是空的,则调用resize()方法扩容
n = (tab = resize()).length;
/**
* tab是一个Node的数组,如果index=i的下标对应的节点是空,则构建一个新的节点并赋值
*/
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//如果index=i的节点不为空
Node<K,V> e; K k;
//如果hash值相同且key值相等,则覆盖value值
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 {
//如果hash值和key都不相等
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;
}
//如果中间存在相同的key,则结束循环
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;
//该方法是保留方法,功能是修改节点的顺序,按照访问的顺序展示,LinkedHashMap做了实现
afterNodeAccess(e);
return oldValue;
}
}
//更新此hashmap结构修改的次数
++modCount;
//更新元素元素个数并判断是否需要扩容
if (++size > threshold)
resize();
//HashMap中没有做实现,LinkedHashMap中做了重写
afterNodeInsertion(evict);
return null;
}
3.2、扩容方法resize()
final Node<K,V>[] resize() {
//当的哈希表
Node<K,V>[] oldTab = table;
//当前的哈希容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//当前的阈值
int oldThr = threshold;
//初始新的容量和阈值为0
int newCap, newThr = 0;
//如果当前容量大于0
if (oldCap > 0) {
//如果当前容量大于等于最大默认容量2的30次方
if (oldCap >= MAXIMUM_CAPACITY) {
//设置最大阈值为2的31次方-1
threshold = Integer.MAX_VALUE;
//直接返回当前哈希表,不进行扩容
return oldTab;
}
//设置新的容量为旧容量的2倍
//同时如果新容量值小于最大默认容量且旧容量值大于等于默认的初始容量(16)
//则设置新的阈值为旧阈值的2倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
//如果当前容量为0当前阈值大于0,说明初始化指定了阈值,设置新阈值=旧阈值
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else {
//如果当前容量和阈值都为0
//则新容量默认使用初始容量,新阈值=默认的加载因子*默认初始容量(0.75*16)
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//如果新的阈值等于0
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) {
//将旧的哈希列表中的元素置为null
oldTab[j] = null;
if (e.next == null)
//如果该节点的下一位为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;
}
3.3、删除方法remove()
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
//如果hash列表不为空,且index对应的有节点
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}