前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >HashMap源码解析(三)

HashMap源码解析(三)

作者头像
小土豆Yuki
发布2021-12-04 11:25:21
发布2021-12-04 11:25:21
19700
代码可运行
举报
文章被收录于专栏:洁癖是一只狗洁癖是一只狗
运行总次数:0
代码可运行

今天我们看看JDK1.8中的HashMap的源码,我们就重点看一下其中的常用方法的源码

Put方法

  • 如果定位到数组位置没有元素,就直接插入
  • 如果定位到数组位置有元素,要和插入的key和hash比较,如果key和hash值相同就直接覆盖,如果key或hash不相同,就判断p是否是一个树节点,则插入树节点,如果还不是就会遍历数组位置上的链表进行插入(尾插入)
代码语言:javascript
代码运行次数:0
复制
    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;
        //table为初始化或者长度为0,则进行扩容
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
         //计算新的节点的下表在数组的那个位置,如果为空,则新生成节点放到数组tab[i]中
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        //如果到了这里说明tab[i]已经存在元素
        else {
            Node<K,V> e; K k;
            //判断当前元素的hash值和数组tab[i]的第一个元素的key是否相等
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
             //然后记录tab[i]第一个元素,记录到e
              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;
                    }
                    //在遍历的过程中,发现链表中有插入的key和链表的key相当,就跳出循环
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e; 
                }
            }
            //这里就是上面获取到的key值和hash值与插入元素相等的节点,然后替换旧值返回旧值
            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方法

  • 首先获取到key对应的数组的下标是否相等,如果相等就直接返回
  • 否则判断是否是一个树节点,如果是则从树中获取
  • 还没找到,就在链表中循环寻找,如果还是没有找到就返回null
代码语言:javascript
代码运行次数:0
复制
    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        //这里判断tab数组不是null且长度大于0
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            //找到对应数组下表,比较key值和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;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 洁癖是一只狗 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档