您的位置:首页 > 其它

hashMap源码解析

2015-11-03 15:51 531 查看
hashMap底层有一个数组存放所有的数据:

  Node<K,V>[] tab;//链表数组

hashMap最重要的两个方法:

put,根据hash值找到位置,如果没有发生冲突,键值对放入数组中,如果发生冲突,需要解决冲突,如果当前已经存在key的映射关系,新value会替换以前的,如果不存在,会把当前键值对放在找到的索引位置链表末端;

/**
* Implements Map.put and related methods
*
* @param hash 根据key计算出来的hash值
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, 不改变已经存在的值
* @param evict if false,表处在创建模式
* @return 之前的值,如果不存在返回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)
n = (tab = resize()).length;

  //如果当前key的hash值没有发生冲突,把它放在数组的最后一个,生成一个新的结点
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e;

    K k;

    //如果hash值相同并且key相同
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不同,不是TreeNode,需要解决冲突,找到一个位置放置,一般是放在当前结点的链表尾部
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;
}

       //如果键和hash都相同,说明已经存在映射关系
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}

    //已经存在当前key的映射,需要替换之前的值,
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;
}


get方法:

1 //根据指定的键来查找值
2
3
4 public V get(Object key) {
5         Node<K,V> e;
6         return (e = getNode(hash(key), key)) == null ? null : e.value;
7     }
8
9 /**
10      * Implements Map.get and related methods
11      *
12      * @param hash hash for key
13      * @param key the key
14      * @return the node, or null if none
15      */
16     final Node<K,V> getNode(int hash, Object key) {
17         Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
18
19    //如果底层数组不为空,并且可以根据hash找到键值对在数组中的位置
20         if ((tab = table) != null && (n = tab.length) > 0 &&
21             (first = tab[(n - 1) & hash]) != null) {
22
23     //总是先检查第一个结点,找到则返回
24             if (first.hash == hash && // always check first node
25                 ((k = first.key) == key || (key != null && key.equals(k))))
26                 return first;
27             if ((e = first.next) != null) {
28                 if (first instanceof TreeNode)
29                     return ((TreeNode<K,V>)first).getTreeNode(hash, key);
30                 do {
31
32        //循环遍历链表,直到找到结点
33                     if (e.hash == hash &&
34                         ((k = e.key) == key || (key != null && key.equals(k))))
35                         return e;
36                 } while ((e = e.next) != null);
37             }
38         }
39         return null;
40     }


分享从伯乐在线看到的一篇好文章 :

java8中hashMap的性能提升
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: