HashMap底层数据结构,扩容

概要

在JDK 1.8中,HashMap的底层数据结构是“数组+链表或者数组+红黑树”,即在链表的长度超过阈值8时,将链表转化为红黑树结构,这样大大减少了查找时间。(因为链表太长了影响查询效率)

在这里插入图片描述
(备注:原图地址https://img2018.cnblogs.com/blog/1439473/201910/1439473-20191012095504421-1009568986.png)

HashMap中put方法源码

HashMap<String, String> map = new HashMap<>();
        map.put("name","tom");

当我们调用put()方法时底层是怎么操作的?
如下是put()方法源码:

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) // -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;
 }

其中里面提到了table resize();

其中table:
源码中这样定义transient Node<K,V>[] table;是一个数组,每个元素都是Node<K,V>。

  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;
      }
      //...
}

Entry是HashMap内部定义的一个私有类,包含了key和value两个属性。HashMap内部使用一个Entry[]数组(源码中table数组)用于存数据,数组的每个元素称为“桶”,每个桶可能存储一个键值对(Entry对象)

其中 resize()作用:
初始化表大小或将表大小加倍。 如果为空,则根据阈值字段中保存的初始容量目标进行分配。 否则,因为我们使用的是 2 的幂扩展,所以每个 bin 中的元素必须保持在相同的索引处,或者在新表中以 2 的幂偏移量移动。(默认初始容量static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

扩容与重新哈希:
HashMap 在初始化时,有一个默认的初始容量(16),并且有一个加载因子(0.75)。
当 HashMap 的大小(已存储的键值对数量)超过容量乘以加载因子时,HashMap 会进行扩容,新的容量是原来的两倍,并且会进行重新哈希,将已经存在的元素重新放入新的 bucket 位置。

小结

HashMap 的底层数据结构包括哈希表(数组)、链表和红黑树。通过哈希表,我们可以快速定位元素的位置;通过链表和红黑树,我们可以解决哈希冲突的问题;通过扩容和重新哈希,我们可以保证 HashMap 的性能。


谢谢大家指点,我会耐心听取各位意见。

觉得有用请点个👍

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值