hashmap原理

  • loadFactor:数据的增长因子,默认为0.75。在进行扩容操作会使用到。
  • threshold:允许的最大的存储的元素数量,通过length数组长度*loadFactor增长因子得出
  • modCount:记录内部结构发生变化的次数,put操作(覆盖值不计算)以及其他...
  • size:实际存储的元素数量

put方法

https://user-gold-cdn.xitu.io/2018/12/21/167cf3bc3724604a?imageView2/0/w/1280/h/960/ignore-error/1

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

                   boolean evict) {

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

        // 判断数组是否为空,长度是否为0,是则进行扩容数组初始化

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

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

        // 通过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;

            // 该数组元素在链表长度>8后形成红黑树结构的对象

            else if (p instanceof TreeNode)

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

            else {

                // 该数组元素hash相等,key不等,同时链表长度<8.进行遍历寻找元素,有就覆盖无则新建

                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

                            // 链表长度>=8 结构转为 红黑树

                            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;

}

resize()操作

https://user-gold-cdn.xitu.io/2018/12/21/167cf3bc730abd73?imageView2/0/w/1280/h/960/ignore-error/1

在扩容操作中,因无需重新计算hash值,同时均匀将链表冲突的元素均匀分布到新的数组中。

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

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

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

 4     int oldThr = threshold;

 5     int newCap, newThr = 0;

 6     if (oldCap > 0) {

 7         // 超过最大值就不再扩充了,就只好随你碰撞去吧

 8         if (oldCap >= MAXIMUM_CAPACITY) {

 9             threshold = Integer.MAX_VALUE;

10             return oldTab;

11         }

12         // 没超过最大值,就扩充为原来的2倍

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

14                  oldCap >= DEFAULT_INITIAL_CAPACITY)

15             newThr = oldThr << 1; // double threshold

16     }

17     else if (oldThr > 0) // initial capacity was placed in threshold

18         newCap = oldThr;

19     else {               // zero initial threshold signifies using defaults

20         newCap = DEFAULT_INITIAL_CAPACITY;

21         newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);

22     }

23     // 计算新的resize上限

24     if (newThr == 0) {

25

26         float ft = (float)newCap * loadFactor;

27         newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?

28                   (int)ft : Integer.MAX_VALUE);

29     }

30     threshold = newThr;

31     @SuppressWarnings({"rawtypes","unchecked"})

32         Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];

33     table = newTab;

34     if (oldTab != null) {

35         // 把每个bucket都移动到新的buckets中

36         for (int j = 0; j < oldCap; ++j) {

37             Node<K,V> e;

38             if ((e = oldTab[j]) != null) {

39                 oldTab[j] = null;

40                 if (e.next == null)

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

42                 else if (e instanceof TreeNode)

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

44                 else { // 链表优化重hash的代码块

45                     Node<K,V> loHead = null, loTail = null;

46                     Node<K,V> hiHead = null, hiTail = null;

47                     Node<K,V> next;

48                     do {

49                         next = e.next;

50                         // 原索引

51                         if ((e.hash & oldCap) == 0) {

52                             if (loTail == null)

53                                 loHead = e;

54                             else

55                                 loTail.next = e;

56                             loTail = e;

57                         }

58                         // 原索引+oldCap

59                         else {

60                             if (hiTail == null)

61                                 hiHead = e;

62                             else

63                                 hiTail.next = e;

64                             hiTail = e;

65                         }

66                     } while ((e = next) != null);

67                     // 原索引放到bucket里

68                     if (loTail != null) {

69                         loTail.next = null;

70                         newTab[j] = loHead;

71                     }

72                     // 原索引+oldCap放到bucket里

73                     if (hiTail != null) {

74                         hiTail.next = null;

75                         newTab[j + oldCap] = hiHead;

76                     }

77                 }

78             }

79         }

80     }

81     return newTab;

82 }

null key的存取

        null key总是存放在Entry[]数组的第一个元素。

private V putForNullKey(V value) {

        for (Entry<K,V> e = table[0]; e != null; e = e.next) {

            if (e.key == null) {

                V oldValue = e.value;

                e.value = value;

                e.recordAccess(this);

                return oldValue;

            }

        }

        modCount++;

        addEntry(0, null, value, 0);

        return null;

    }



    private V getForNullKey() {

        for (Entry<K,V> e = table[0]; e != null; e = e.next) {

            if (e.key == null)

                return e.value;

        }

        return null;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值