Hashmap
存储结构
内部使用Node数组table存储1
transient Node<K,V>[] table;
Node结构存储键值对,next可以看作是指针,所以Node可以看做链表节点,hashmap也使用拉链法来解决冲突,把数组的每个位置当作一个桶,一个桶存一个链表。这个桶中存放key的hash值相同的节点。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
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;
}
}
put操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
public V put(K key, V value) {
//put方法实际上是调用了putVal方法
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key //key的hash值
* @param key the key //key的值
* @param value the value to put //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 //如果有这个key,返回之前的value,如果没有这个key存在,返回null
*/
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) //初始化或者扩容底层table
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null) //给节点p赋值 给索引i赋值
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) {
//拉链法解决hash冲突 从索引位置节点搜索,直到搜索到next为空的节点 把这个节点的next指向put的节点
p.next = newNode(hash, key, value, null);
//链表长度大于等于8 转成红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//找到这条链上的节点如果有key一样的 就直接可以退出循环了 进入下面替换value的过程了
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key 找到key一样的节点了,直接返回旧的value把新的value赋值给节点
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold) //看大小与容量的关系决定是否扩容
resize();
afterNodeInsertion(evict);
return null;
}
}
get操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
//根据hash值找到所在位置,根据这条链找 找到key一样的node,返回value,如果没有就返回null
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) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(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;
}
扩容
初始容量16,达到阀值扩容,阀值为最大容量*负载因子,扩容后的容量为原来的2倍,总是为2的n次方
jdk1.7和1.8的resize区别:
1.7中的需要重新用hash和数组长度计算桶下标,而1.8不需要重新计算,只需要看数组长度&hash值以后新增的位是0还是1,如果是0,就还是在原来的索引,如果是1,位置就是
原来的索引+oldCap