zoukankan      html  css  js  c++  java
  • HashMap 数据结构分析

    测试代码

    public class App {
        public static void main(String[] args) {
            System.out.println("123");
            Map<Object, Object> hashMap = new HashMap<>(4);
    
            for (int i = 0; i < 64; i++) {
                hashMap.put(i, i);
            }
    
            for (int i = 0; i < 9; i++) {
                hashMap.put(new User(), i);
            }
    
    
            hashMap.get("hi");
        }
    
        private static class User {
            private int hash;
            @Override
            public int hashCode() {
                hash = 123;
                return 123;
            }
        }
    
    }
    

    数据结构分析

    HashMap{
      // 数组,若没有给定初始值,第一次 put 时才会初始化,
      /**
       	 resize() {
    	   int oldThr = threshold;
    	   ...
    	   // 给初始化容量大小
    	   else if (oldThr > 0) 
         	newCap = oldThr;
         // 不给则默认为 16 DEFAULT_INITIAL_CAPACITY = 1 << 4;
         else {          
                newCap = DEFAULT_INITIAL_CAPACITY;
                newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
         }
         ...
    		 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
       }
       **/
      transient Node<K,V>[] table;
    	// 单向链表
      Node.next
    }
    

    逻辑分析

    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;
              // hash 相等 Key 相等则覆盖
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
              // 变红黑树之后,即在 treeifyBin(tab, hash) 方法中的                TreeNode<K,V> p = replacementTreeNode(e, null); 执行之后;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
              // hash 冲突
                else {
                  // 某个键出现 hash 冲突时,会使用(Node.next属性)进行链接(链表)。当长度 >=8 ,并且数组长度大于 MIN_TREEIFY_CAPACITY = 64时,此时链表转为红黑树,即 treeifyBin 方法中的replacementTreeNode();
                    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);
                            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);
            }
        }
    

    红黑树成果图

    image-20210918081913571

    验证

    链表超过8个,但是元素不足64,不会使用红黑树

    image-20210918082318302

    只会 resize
    image-20210918082408875

    preserve order

    image-20210918082752692

    换了个位置
    image-20210918082947204

    image-20210918083217414

  • 相关阅读:
    Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压
    Codeforces Round #358 (Div. 2) D. Alyona and Strings 字符串dp
    Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心
    Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
    Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
    Codeforces Gym 100342J Problem J. Triatrip 三元环
    HDU 4587 TWO NODES 割点
    hdu 5615 Jam's math problem(十字相乘判定)
    C++数组作为函数参数的几个问题(转)
    UVA
  • 原文地址:https://www.cnblogs.com/sethxiong/p/15329882.html
Copyright © 2011-2022 走看看