HashMap源码分析(七)

举报
知识浅谈 发表于 2022/10/24 10:15:01 2022/10/24
【摘要】 HashMap源码分析(七)

在这里插入图片描述

🍁 作者:知识浅谈,CSDN博客专家,阿里云签约博主,InfoQ签约博主,华为云云享专家,51CTO明日之星
📌 擅长领域:全栈工程师、爬虫、ACM算法
💒 公众号:知识浅谈

HashMap源码分析(六)总结
🤞这次都给他拿下🤞

正菜来了⛳⛳⛳

🎈HashMap源码的函数

🍮boolean replace()

含义:把map中的对应的key的旧value替换成新value值,具体的操作就是先查询key对应键值对节点的引用,然后判断节点是否为对应的value的对象,最后把Node节点的对象的value赋值为新值,如果操作成功就返回true,否则返回false。

public boolean replace(K key, V oldValue, V newValue) {
    Node<K,V> e; V v;
    if ((e = getNode(hash(key), key)) != null &&
        ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
        e.value = newValue;
        afterNodeAccess(e);
        return true;
    }
    return false;
}

🍮V replace(K key, V value)

含义:这个和上边的含义一样,根据key查找对应的Node节点,把新值赋值给该节点,并返回旧值。

public V replace(K key, V value) {
    Node<K,V> e;
    if ((e = getNode(hash(key), key)) != null) {
        V oldValue = e.value;
        e.value = value;
        afterNodeAccess(e);
        return oldValue;
    }
    return null;
}

🍮V computeIfAbsent()

含义:第一个参数是hashMap的key,第二个参数是一个方法,叫做重新映射函数,用于重新计算值(就是说value值是这个方法重新计算后的结果),返回值:返回的就是value值。

public V computeIfAbsent(K key,
                         Function<? super K, ? extends V> mappingFunction) {
    if (mappingFunction == null)
        throw new NullPointerException();
    int hash = hash(key);
    Node<K,V>[] tab; Node<K,V> first; int n, i;
    int binCount = 0;
    TreeNode<K,V> t = null;
    Node<K,V> old = null;
    if (size > threshold || (tab = table) == null ||
        (n = tab.length) == 0)
        n = (tab = resize()).length;
    if ((first = tab[i = (n - 1) & hash]) != null) {
        if (first instanceof TreeNode)
            old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
        else {
            Node<K,V> e = first; K k;
            do {
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k)))) {
                    old = e;
                    break;
                }
                ++binCount;
            } while ((e = e.next) != null);
        }
        V oldValue;
        if (old != null && (oldValue = old.value) != null) {
            afterNodeAccess(old);
            return oldValue;
        }
    }
    V v = mappingFunction.apply(key);
    if (v == null) {
        return null;
    } else if (old != null) {
        old.value = v;
        afterNodeAccess(old);
        return v;
    }
    else if (t != null)
        t.putTreeVal(this, tab, hash, key, v);
    else {
        tab[i] = newNode(hash, key, v, first);
        if (binCount >= TREEIFY_THRESHOLD - 1)
            treeifyBin(tab, hash);
    }
    ++modCount;
    ++size;
    afterNodeInsertion(true);
    return v;
}

🍮merge(key,value,remappingFunction)

merge() 方法会先判断指定的 key 是否存在,如果不存在,则添加键值对到 hashMap 中,如果不存在,则返回通过 remappingFunction 重新计算后的值。
代码较多,就不再拿上来,具体的可自行查看。

🍮void replaceAll()

含义:这个函数主要含义是把map对象中的key和value等都用传入的函数重新计算,并复制给指定的节点,下方的function.apply函数就是主要的方法重新计算value值赋值给e的value。

public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
    Node<K,V>[] tab;
    if (function == null)
        throw new NullPointerException();
    if (size > 0 && (tab = table) != null) {
        int mc = modCount;
        for (int i = 0; i < tab.length; ++i) {
            for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                e.value = function.apply(e.key, e.value);
            }
        }
        if (modCount != mc)
            throw new ConcurrentModificationException();
    }
}

🍮Object clone()

含义:这个函数的主要作用事克隆一个当前对象的复制品,,但是具体的实现还是调用的super的clone方法,返回一个新对象,这种克隆一般都是浅拷贝,并且克隆新对象之后,回再调用reinitialize函数尽心初始化,调用putMapEntries函数。

public Object clone() {
    HashMap<K,V> result;
    try {
        result = (HashMap<K,V>)super.clone();
    } catch (CloneNotSupportedException e) {
        // this shouldn't happen, since we are Cloneable
        throw new InternalError(e);
    }
    result.reinitialize();
    result.putMapEntries(this, false);
    return result;
}

🍚总结

以上就是关于HashMap源码的部分函数的实现解析,希望有所帮助。Written By 知识浅谈

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。