public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
transient Node<K,V>[] table;
transient int size;
transient int modCount;
static 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;
}
... ...
}
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, 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)
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) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1)
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) {
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
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 &&
((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;
}
... ...
... ...
}
- Node《K,V》 table就是Node数组(Node是HashMap的内部类,实现自Map.Entry《K,V》;成员属性有key,value分别存键和值;Node类型的next成员,在产生冲突时形成链表存入一个指定槽中),向HashMap里添加的一对新的键值对,就是向这个table即添加一个新的Node元素。
- 构建以及插入的步骤:计算得到键值对中键的hash码(HashMap中的hash()方法),再经过计算(
i = (n - 1) & hash
)得到这个新的键值对作为table数组中的下标,如果这个位置bucket(一般称数组中的每个位置为槽)已经有了元素,说明键值对中键的hash码和新插入键值对中键的hash码,这时判断这两个键是否相同(具体取决于键的类型K的equals方法和 == ),相同则新键值对替换旧的键值对并返回旧的键值对中的value。不同则,使用链表的形式,将新的Node追加到原有Node后面。最后两种情况都要modcount修改次数加1. - get的步骤:首先是获取你想要的Node,有了Node返会
Node.value
直接就是你想要的值了。
getNode的步骤是:由hashCode经过计算得到在数组中的位置index,取出这个index对应槽中存的Node节点,如果有冲突的话该Node节点后面还有接Node节点形成链表,如果没有冲突的话,就只有这个Node节点。从第一个节点向后遍历,判断这一个个节点哪个的key和用户传进来的key相等(取决于key类型的equals方法和 ==),只有节点的hashCode和用户传来key的hashCode相等并且两者key值相等,才是真正要返回的节点。