HashMap代码分析
注:本文只分析 HashMap 的 put 操作和 get 操作,后续实现也将只实现其 put 方法。本文源码基于JDK1.8。
put 操作
先看put操作的代码实现:
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) // -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;
}
put 方法内就是调用了这个 putVal 方法。
该方法首先将全局的Node[]对象table赋给了方法内的局部变量,并判断其是否需要初始化。首次使用这个HashMap对象时,需要会初始化这个table对象,创建一个长度为16的Node[]数组。
随后,会通过 (n - 1) & hash 表达式,确定key在数组中的位置。其中,n 变量为table数组长度,hash 变量为key的hash值。如果数组上该位置的没有元素,则通过 newNode 方法,创建一个元素,并放入该位置。(newNode 方法就是创建一个Node()对象。HashMap 中的每个键值对都是一个Node对象)
如果计算得出的存储位置上不为空,也就是table数组的当前位置上已经存在元素,则首先会判断这个元素与要存储的元素的key值是否相同,判断的方式是:当前位置的hash值与要保存的key的hash值相同及两个key的内容相同。如果相同,就用的新的value覆盖就得value。说明:由于两个内容不同的key可能会产生相同的hash值,则table数组中的每个位置都用链表或树结构来保存 。
如果不同,则会先判断 table[(n - 1) & hash] 位置的元素是否为树形结构(超过阀值将采用红黑树的结构保存数据)。
如果没有不需要转换为红黑树,则会将新元素加入到链表中。
最后,会判断当前 table 数组是否需要扩容,如果需要则调用 resize 方法,容量会变为旧数组的二倍。
get 操作
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;
}
getNode 方法首先也是根据 (n - 1) & hash 表达式确定所查找的元素的存放位置,再遍历链表(如果当前位置还是链结构),根据 hash 值和 key 的内容来判断当前的key与要查找的key是否相同,如果相同,便返回当前的 Node 对象。再在 get 方法中返回Node.value 即可。
实现自己的HashMap,原理与HashMap类似
public void putVal(int hash, Object key, Object value){
Element[] e;
Element p;
int length = 0;
int i;
if((e = table) == null || (length = table.length) == 0) {
e = resize(length);
length = e.length;
}
if((p = e[i = (length-1) & hash]) == null){
e[i] = new Element(key,value,hash,null,null);
}else{
if(p.hash == hash && (p.key == key || key.equals(p.key))) {
p.value = value;
return;
}else{
Element t = p.next;
Element temp = new Element(key,value,hash,p,p.next);
p.next = temp;
t.prev = temp;
}
}
size++;
if (size >= table.length)
resize(size);
}
class Element{
Object key;
Object value;
int hash;
Element prev;
Element next;
public Element(Object key,Object value,int hash, Element prev, Element next){
this.key = key;
this.value = value;
this.hash = hash;
this.prev = prev;
this.next = next;
}
public String toString(){
return key + "=" + value;
}
}
MyHashMap 类与 HashMap 的put方法的不同之处在于,如果发生hash碰撞时,在每个table数组元素中用双向链表来保存数据,并直接将元素放入链表头节点之后的位置。我在 Element 对象中,也维护了其前驱和后继节点。并且没有扩容因子的限制,当 table 数组大小达到上限时,就进行扩容。
get 操作与 HashMap 的 get 方法相同,这里就不再说明。