HashMap_jdk1.8源码分析

如果需要满足线程安全,可以用Collections的synchronizedMap方法使HashMap具有线程安全的能力,或者使用ConcurrentHashMap

 

下面我们讲解下JDK1.8做了哪些优化。经过观测可以发现,我们使用的是2次幂的扩展(指长度扩为原来2倍),所以,元素的位置要么是在原位置,要么是在原位置再移动2次幂的位置。看下图可以明白这句话的意思,n为table的长度,图(a)表示扩容前的key1和key2两种key确定索引位置的示例,图(b)表示扩容后key1和key2两种key确定索引位置的示例,其中hash1是key1对应的哈希与高位运算结果。

 

当一个链表太长的时候,HashMap会动态的将它替换成一个红黑树,这话的话会将时间复杂度从O(n)降为O(logn)。

 

 

1. 确定哈希桶数组索引位置

方法一:jdk1.8

static finalint hash(Object key) {   //jdk1.8 & jdk1.7

     inth;

     //h = key.hashCode() 为第一步 取hashCode值

     //h ^ (h >>> 16)  为第二步 高位参与运算

     return(key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

}

方法二:

static int indexFor(inth, int length) {  //jdk1.7的源码,jdk1.8没有这个方法,但是实现原理一样的

     returnh & (length-1);  //第三步 取模运算

}

这里的Hash算法本质上就是三步:取key的hashCode值、高位运算、取模运算。

Node(int hash, K key, Vvalue, Node<K,V> next) {

            this.hash = hash;

            this.key = key;

            this.value = value;

            this.next = next;

        }

 

public final inthashCode() {

            return Objects.hashCode(key) ^Objects.hashCode(value);

        }

 

2. 分析HashMap的put方法

 

static final inthash(Object key) {   //jdk1.8 & jdk1.7

     inth;

     //h = key.hashCode() 为第一步 取hashCode值

     //h ^ (h >>> 16)  为第二步 高位参与运算

     return(key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

}

 

public V put(K key, Vvalue) {

 2    // 对key的hashCode()做hash

 3    return putVal(hash(key), key, value, false, true);

 4 }

 5

 6 final VputVal(int hash, K key, V value, boolean onlyIfAbsent,

 7               boolean evict) {

 8    Node<K,V>[] tab; Node<K,V> p; int n, i;

 9    // 步骤①:tab为空则创建

10    if ((tab = table) == null || (n = tab.length) == 0)

11        n = (tab = resize()).length;

12    // 步骤②:计算index,并对null做处理

13    if ((p = tab[i = (n - 1) & hash]) == null)

14        tab[i] = newNode(hash, key, value, null);

15    else {

16        Node<K,V> e; K k;

17        // 步骤③:节点key存在,直接覆盖value

18        if (p.hash == hash &&

19            ((k = p.key) == key || (key != null && key.equals(k))))

20            e = p;

21        // 步骤④:判断该链为红黑树

22        else if (p instanceof TreeNode)

23            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

24        // 步骤⑤:该链为链表

25        else {

26            for (int binCount = 0; ; ++binCount) {

27                if ((e = p.next) == null) {

28                    p.next = newNode(hash, key,value,null);

                        //链表长度大于8转换为红黑树进行处理

29                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st 

30                        treeifyBin(tab, hash);

31                    break;

32                }

                    //key已经存在直接覆盖value

33                if (e.hash == hash &&

34                    ((k = e.key) == key || (key != null &&key.equals(k))))                                           break;

36                p = e;

37            }

38        }

39        

40        if (e != null) { // existing mapping for key

41            V oldValue = e.value;

42            if (!onlyIfAbsent || oldValue == null)

43                e.value = value;

44            afterNodeAccess(e);

45            return oldValue;

46        }

47    }

 

48    ++modCount;

49    // 步骤⑥:超过最大容量 就扩容

50    if (++size > threshold)

51        resize();

52    afterNodeInsertion(evict);

53    return null;

54 }

3. 扩容机制

在hashmap扩容的时候,如果遇到多线程访问,可能产生条件竞争(race condition)。

当重新调整HashMap大小的时候,确实存在条件竞争,因为如果两个线程都发现HashMap需要重新调整大小了,它们会同时试着调整大小。在调整大小的过程中,存储在链表中的元素的次序会反过来,因为移动到新的bucket位置的时候,HashMap并不会将元素放在链表的尾部,而是放在头部,这是为了避免尾部遍历(tail traversing)。如果条件竞争发生了,那么就死循环了。


 final Node<K,V>[] resize() {

        Node<K,V>[] oldTab = table;

        int oldCap = (oldTab == null) ? 0 :oldTab.length;

        int oldThr = threshold;

        int newCap, newThr = 0;

//当桶中存储有元素

//如果桶的数量大于最大值,将桶的数量设置成整数做大值

        if (oldCap > 0) {

            if (oldCap >= MAXIMUM_CAPACITY){

                threshold = Integer.MAX_VALUE;

                return oldTab;

            }

//否则,判断桶的数量是多少。如果小于最大值,大于默认值。如果左移一位还是小于最大值,之后左移一位。

            else if ((newCap = oldCap <<1) < MAXIMUM_CAPACITY &&

                     oldCap >=DEFAULT_INITIAL_CAPACITY)

                newThr = oldThr << 1; //double threshold

        }

//桶中没有存储元素

        else if (oldThr > 0) // initialcapacity was placed in threshold

            newCap = oldThr;

        else {               // zero initial thresholdsignifies using defaults

            newCap = DEFAULT_INITIAL_CAPACITY;

            newThr = (int)(DEFAULT_LOAD_FACTOR* DEFAULT_INITIAL_CAPACITY);

        }

        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) {

                    oldTab[j] = null;

   //如果桶为空,直接插入桶中

                    if (e.next == null)

                        newTab[e.hash &(newCap - 1)] = e;

   //如果桶中存储的是红黑树。插入树中

                    else if (e instanceofTreeNode)

                       ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);

  //如果桶中存储的是链表。插入到链表中。由于桶的容量发生改变。所以可能会发生hash值得变化。导致在不同的链表中。

                    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;

                        }

//

//原索引+oldCap放到bucket里

                        if (hiTail != null) {

                            hiTail.next = null;

                            newTab[j + oldCap] =hiHead;

                        }

                    }

                }

            }

        }

        return newTab;

    }

4.get()方法

当我们调用get()方法,HashMap会使用键对象的hashcode找到bucket位置,然后获取值对象。如果bucket中只有一个值,判断是否是我们需要的。通过hash和key进行对比。如果有多个值对象储存在同一个bucket,判断bucket中存储的是链表还是红黑树。然后遍历查找。

 public 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

     */

    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) {

    //判断hash的槽存不存在,并且是不是有数据,且只有一个数据

            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;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值