Map-HashMap

1.HashMap

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FFqZE7WH-1624158547789)(java知识点/image-20210618131051961.png)]

点开源码,瞧,发现了啥?维护的是一个链表数组,同样的hash值在一个table[i]中。

当链表的长度大于最小”建树阈值的时候“,就会变成维护一个树。

transient Node<K,V>[] table;
static final int TREEIFY_THRESHOLD = 8;
    Node(int hash, K key, V value, Node<K,V> next) {
        this.hash = hash;
        this.key = key;
        this.value = value;
        this.next = next;
    }

    public final K getKey()        { return key; }
    public final V getValue()      { return value; }
    public final String toString() { return key + "=" + value; }

    public final int hashCode() {
        return Objects.hashCode(key) ^ Objects.hashCode(value);
    }

    public final V setValue(V newValue) {
        V oldValue = value;
        value = newValue;
        return oldValue;
    }

    public final boolean equals(Object o) {
        if (o == this)
            return true;
        if (o instanceof Map.Entry) {
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
            if (Objects.equals(key, e.getKey()) &&
                Objects.equals(value, e.getValue()))
                return true;
        }
        return false;
    }
}

在这里插入图片描述

大概存储的方式就是如图这样,数组里面是个链表,链表过长,就会变成一个树。

因为TreeNode的父类的父类继承了Node,所以这个Node数组也可以存储TreeNode

键值对与数组下标的对应关系由Key值的hashCode来决定。

即,求出了的hashCode假如为 6, 那么这个存有value值的Node对象就存在table[6]

实际上源码里的还要tab[i = (n - 1) & hash],n是数组的长度。

当然了,数组扩增的时候,数组长度n会变,在resize这个方法里也会改变旧的数组的位置的,寻找的时候不会出现错误的。

啥是哈希冲突?

就是hashcode值重复了。

查看以下HashMap的基本功能实现原理

put方法

public V put(K key, V value) {
    //put方法会调用putVal方法
    return putVal(hash(key), key, value, false, true);
}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
    	//创建指向HashMap维护的Node数组,如果数组不存在,就要调用resize进行初始化
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
    	//如果根据数组长度减一和hash值进行位与的位置没有Node占领,那么就直接赋给这个Node这个位置。
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
    	//否则的话
        else {
            Node<K,V> e; K k;
            //根据hash值判断,传入的key与数组中存在的Key的值是否相等,如果相等,让当前的引用指向数组中的Node对象
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            //key值不同,那么看看数组中装的是否位TreeNode对象,如果是说明已经建树了,调用putTreeVal
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            //当前面情况都不满足的时候,说明当前下标节点有链表节点,此时遍历链表,判断key值是否重复,以判断是替换某个节点还是
            //增加
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        //如果超过建树阈值,那么就要调用treeifyBin把链表重构为树
                        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;
}

get方法

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;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                //如果是树了,调用getTreeNode,查找相应的树节点
                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;
    }
关于HashMap中的红黑树(TODO)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值