HashMap原理分析

数组

数组是一块内存连续,逻辑也连续的一组数据。在Java里需要类型一样,JS里可以类型不一致

List

内存不连续,但是逻辑连续。意思就是:在物理内存上存储并不是连续的,但是在逻辑上,下标为1的元素的下一个元素一定是下标为2

LinkedList

  • 单链表:一个node的next指向下一个node,然后传递。
  • 双链表结构数据:一个node的pre指向上一个node,node的next指向想一个node,依次传递。

HashMap

结合了数组和链表的优点

  • 核心成员变量,本身是一个数组,其元素又是一个单链表

    transient Node<K,V>[] table;
    
  • 核心静态内部类实现了Map.Entry<K,V>

            Node(int hash, K key, V value, Node<K,V> next) {
                this.hash = hash;
                this.key = key;
                this.value = value;
                this.next = next;
            }
    
    • hash,key的hash,hash(key),计算出来是key的下标,也叫桶的下标
    • key,单个元素的键
    • value,单个元素的值
    • next,指向下一个node
  • 核心算法 hash算法,这个算法主要是通过key的hashcode计算一个合理值

    static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    

    在put的时候,有一步是计算把node放到哪个位置的时候,用到了(n - 1) & hash,这个hash即使通过hash算法求出来的。对整个公式详细解读如下:

    • key的hashCode()是个很大的数,一般情况下是32位的二进制
    • h >>> 16,无符号右移16位,让高16为都为0
    • (h = key.hashCode()) ^ (h >>> 16),这样,得出来的数值,在低16为更均匀,因为table的个数(n,但是用了n-1更妙)不会太大,低16为一般可以满足。这也是为什么不左移16为的原因,如果左移16则低16为数据会出现很多0,与n-1异或时会导致数据扎堆
    • (n - 1) & hash,其实这里的hash的高16位基本用不到,因为n-1不会太大。
    • n-1 ,之所以用n-1是因为奇数的二进制1会更多一点,&操作的结果0就会少一点,如果用n,n的二进制会出现大量0导致&操作的结果大多数是0或者n。如果是0,则元素都会集中到下标为0的桶内,如果是n(不会越界)则会扩容。
  • HashMap的存储原理或者说模型,从put函数可分析

        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;// 如果table为空,则扩容
            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) {
                            // 便利当前桶的所有节点,当前桶内节点属性next为空指向当前值,即后插
                            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;
        }
    
  • 取值,通过放值和取值,就可以完全理解HashMap的结构和流程

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

结构图

  • 从包容的角度给人的感觉是这样的结构
    在这里插入图片描述

  • 其实应该是这种结构
    在这里插入图片描述

  • hashmap总的结构图

    在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值