HashMap底层实现原理

本文详细介绍了Java HashMap在JDK1.8中的实现,包括使用数组加链表以及红黑树的数据结构。当冲突发生时,HashMap会通过hashCode和equals方法处理。如果链表长度超过7且数组长度大于64,HashMap会将链表转换为红黑树以提高查询效率。此外,还展示了put操作的源码解析,包括扩容、节点插入及修改等细节。
摘要由CSDN通过智能技术生成

jdk1.7之前Hashmap使用数组加链表实现。

jdk1.8之后引入了红黑树。红黑树,左子树比结点小,右子树比结点大,提高了查询的效率。

Hashmap的put操作(JDK1.8)

1.计算添加对象的hashCode值(类中应该重写hashCode值和equals方法),若hashCode经过一定的算法计算与原数组已经有了的对象冲突,需要比较hashCode值,若hashCode值不同,原有对象以链表形式指向后添加的对象,添加成功。

2.若hashCode值相同,再调用equals方法,比较是否相同,若不相同,同样以链表形式添加其后。

3.若调用equals方法相同,则put方法的实际作用是将相同对象的原value值改为新对象的value值,即修改作用。

4.当使用put操作,当链表长度达到>=7时,判断数组长度是否大于64,若不大于就扩容,大于就变红黑树。

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

红黑树方法块

final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
            resize();
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            TreeNode<K,V> hd = null, tl = null;
            do {
                TreeNode<K,V> p = replacementTreeNode(e, null);
                if (tl == null)
                    hd = p;
                else {
                    p.prev = tl;
                    tl.next = p;
                }
                tl = p;
            } while ((e = e.next) != null);
            if ((tab[index] = hd) != null)
                hd.treeify(tab);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值