IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

HashMap:源代码(构造方法、put、resize、get、remove、replace)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、常量

(1)缺省table大小,1左移四位变为8

 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

(2)table最大长度

 static final int MAXIMUM_CAPACITY = 1 << 30;

(3)缺省负载因子大小:

 static final float DEFAULT_LOAD_FACTOR = 0.75f;

(4)树化阈值

    static final int TREEIFY_THRESHOLD = 8;

(5)树降级成为链表阈值

    static final int UNTREEIFY_THRESHOLD = 6;

(6)哈希表的元素达到64个之后就升级为树

  static final int MIN_TREEIFY_CAPACITY = 64;

(7)散列表(哈希表)

  transient Node<K,V>[] table;

(8)当前哈希表元素个数:

 transient int size;

(9)当前哈希表修改次数

 transient int modCount;

(10)扩容阈值:当哈希表的元素超过阈值的时候,触发扩容

    int threshold;

(11)负载因子

  final float loadFactor;

2、构造方法

(1)构造方法:

    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

initialCapacity必须是大于零的,最大值是MAXIMUM_CAPACITY,loadFactor也必须是大于零的,这些代码就是一些校验

    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

返回一个大于或等于当前值cap的一个数字,并且这个数字一定是2的次方数。

cap=10

n=10-1=9(如果不减一的话会变成它的二倍)

1001 | 0100(右移一位) = 1101

1101 | 0011(右移两位)=1111

1111 | 0000(右移四位)=1111

…. …

… …

return 15+1=16(加1后,进一位,后面的几位都变成零)

3、put方法

(1)是一个套娃的方法:调用putVal方法:

  public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

hash方法:hashCode经过扰动之后得到一个哈希值,哈希值与表的长度进行运算得到在哈希表中的位置,这个扰动函数就是hash函数。

扰动函数:让key的哈希值的高十六位也参与路由运算

(2)hash方法

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

如果key为空,哈希值就为零,不为空的话,就让h和h右移16位相与,目的是让key的哈希值的高十六位也参与运算:

35_1.png

相与之后相当于将h的高十六位与低十六位相与,如果hashtable不是很大的情况下(16),可以让高十六位参与进来

(3)putVal方法

 final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
//tab:引用当前hashMap的散列表
//p:表示当前散列表的元素
//n:表示散列表数组的长度
//i:表示路由寻址的结果
        Node<K,V>[] tab; Node<K,V> p; int n, i;
//延迟初始化逻辑,第一次调用putValue的时候会初始化hashMap对象中的最耗费内存的散列表
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
//最简单的一种情况:寻址找到的桶位,刚好是null,这个时候,直接将当前k--v=>node扔进去就可以了
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
//e:不为null的话,找到了一个与当前要插入的key value一致的key的元素
//k:表示临时的一个key
            Node<K,V> e; K k;
//表示桶位中的该元素,与当前插入的元素的key完全一致,表示后续需要进行替换操作
            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 {
//链表的情况,而且链表的头元素与我们要插入的key元素不一致
                for (int binCount = 0; ; ++binCount) {
//条件成立的话,说明迭代到最后一个元素了,也没有找到一个与你要插入的key一致的node
//说明需要加入到当前链表区的末尾
                    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的元素,需要进行替换操作
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
//e不等于null,条件成立说明找到了一个与你插入的key完全一致的数据,需要进行替换
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
//表示散列表结构被修改的次数,替换node元素的value不计数
        ++modCount;
//插入新元素,如果自增后的值大于扩容阈值,则触发扩容
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

如果存在key和当前key相同的话,就不再插入了

4、resize方法

(1)源码

final Node<K,V>[] resize() {
//oldTab,引用扩容前的哈希表
        Node<K,V>[] oldTab = table;
//oldCap表示扩容前table数组的长度
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
//oldThr表示扩容之前的扩容阈值,触发本次扩容的阈值
        int oldThr = threshold;
//newCap:扩容之后Table数组的大小
//newThr:扩容之后,下次再次触发扩容的条件
        int newCap, newThr = 0;
//如果条件成立,说明hashMap中的散列表已经初始化过了,是一次正常的扩容
        if (oldCap > 0) {
//扩容之前的table数组大小已经达到最大阈值后,则不扩容,且设置最大扩容条件为int最大值
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
//oldCap左移一位实现数组翻倍,并且赋值给newCap,newCap小于数组最大值限制且扩容前的阈值>=16
//这种情况下则下一次扩容的阈值等于当前阈值翻倍 
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold
        }
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;//16
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) {
            float ft = (float)newCap * loadFactor;
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
        threshold = newThr;
        @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
        table = newTab;
//说明HashMap本次扩容之前,table不为空
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
//当前Node结点
                Node<K,V> e;
//说明当前桶位中有数据,但是数据具体是单个数据还是链表、红黑树并不知道
                if ((e = oldTab[j]) != null) {
//方便jvm GC的时候回收内存
                    oldTab[j] = null;
//第一种情况,当前桶位只有一个元素,从未发生过碰撞,这种情况直接计算出当前元素应该存放在新数组中的位置,然后扔进去就可以了
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
//第二种情况,当前节点已经树化
                    else if (e instanceof TreeNode)
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    else { // preserve order
//第三种情况:桶位已经形成链表

//低位链表:存放在扩容之后的数组的下标位置,与当前数组的下标位置一致
                        Node<K,V> loHead = null, loTail = null;
//高位链表:存放在扩容之后的数组的下标位置为:当前数组下表位置*扩容之前数组的长度
                        Node<K,V> hiHead = null, hiTail = null;
                        Node<K,V> next;
                        do {
                            next = e.next;
                            if ((e.hash & oldCap) == 0) {
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {
                                if (hiTail == null)
                                    hiHead = e;
                                else
                                    hiTail.next = e;
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }

5、get方法

(1)get方法:

   public V get(Object key) {
        Node<K,V> e;
//存进去的时候哈希一下,取出来的时候也要hash
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

(2)getNode方法:

  final Node<K,V> getNode(int hash, Object key) {
//tab:引用当前hashMap的散列表
//first:桶位中的头元素
//e:临时node元素
//n:table数组长度
        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) {

//第一种情况:定位出来的桶位元素,即为咱们要get到的数据
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;
    }

6、remove方法

(1)remove方法:

   public V remove(Object key) {
        Node<K,V> e;
        return (e = removeNode(hash(key), key, null, false, true)) == null ?
            null : e.value;
    }

(2)removeNode方法:

final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {
//tab:引用当前hashMap中的散列表
//p:当前node元素
//n:散列表数组长度
//index:寻址结果
        Node<K,V>[] tab; Node<K,V> p; int n, index;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (p = tab[index = (n - 1) & hash]) != null) {
//说明路由的桶位是有数据的,需要进行查找操作并删除
//node:查找到的结果
//e:当前node的下一个元素
            Node<K,V> node = null, e; K k; V v;
//第一种情况:当前桶位中的元素即为要删除的元素
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            else if ((e = p.next) != null) {
//说明当前的桶位要么是链表要么是红黑树
                if (p instanceof TreeNode)
//判断当前桶位是否升级为红黑树了
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                else {
//链表的查找
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            node = e;
                            break;
                        }
                        p = e;
                    } while ((e = e.next) != null);
                }
            }
//判断node,不为空的话,说明按照key查找需要删除的元素
            if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
//第一种情况,node是树节点,说明需要进行树节点的移除操作
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
//第二种情况,桶位元素即为查找结果,即将该元素的下一个元素移动到桶位中
                else if (node == p)
                    tab[index] = node.next;
                else
//第三种情况:将当前元素p的下一个元素设置成要删除元素的下一个元素

                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

7、repalce方法

   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;
    }

文章永久链接:https://tech.souyunku.com/?p=22556


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(89) 打赏



未经允许不得转载:搜云库技术团队 » HashMap:源代码(构造方法、put、resize、get、remove、replace)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367