HashMap(jdk1.8)

1.類聲明:

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {}

2.變量:

//默認初始化容量
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
    //最大容量
    static final int MAXIMUM_CAPACITY = 1 << 30;
    //默認加載因子
     static final float DEFAULT_LOAD_FACTOR = 0.75f;
     //對於table裏面的每個列表,從鏈表結構轉爲紅黑樹結構的閾值
     static final int TREEIFY_THRESHOLD = 8;
     //對於table裏面的每個列表,從紅黑樹結構轉爲列表結構的閾值
     static final int UNTREEIFY_THRESHOLD = 6;
     //最小樹化容量。只有當表的容量達到這個閾值,才能從列表結構轉爲樹結構
     static final int MIN_TREEIFY_CAPACITY = 64;
     //當改變了表的映射數目(put方法增加了新的Node節點)或者修改了表結構(remove(),clear())的時候,進行+1,若是對map進行迭代的時候,發生modCount不是期望的modCount,即被修改了,則拋出ConcurrentModificationException異常
      transient int modCount;

3.方法:

//找到不小於cap的最小的2的n次方,這個算法之精闢令人歎爲觀止。
具體可見參考文章1。通過右移使得cap-1表示的二進制數每一位都變成1。最後加1得到所需數。
  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;
    }
    //put方法
      public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    //若onlyIfAbsent爲true,不會覆蓋掉舊值,若爲false,會覆蓋掉舊值
     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)//如果是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);//往鏈表尾部插入節點,jdk7是往鏈表頭部插入節點。
                        //當鏈表數量大於樹化閾值時,進行樹化操作,對應的當remove的時候會有一個鏈表化的操作
                        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;
                }
            }
            //如果找到了舊的數據,則根據onlyIfAbsent值決定是否更新
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;//當增加了新的node時,修改次數+1
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
    //往樹結構中插入數據
     final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
                                       int h, K k, V v) {
            Class<?> kc = null;
            boolean searched = false;
            TreeNode<K,V> root = (parent != null) ? root() : this;
            for (TreeNode<K,V> p = root;;) {
                int dir, ph; K pk;
                if ((ph = p.hash) > h)
                    dir = -1;
                else if (ph < h)
                    dir = 1;
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
                    return p;
                else if ((kc == null &&
                          (kc = comparableClassFor(k)) == null) ||
                         (dir = compareComparables(kc, k, pk)) == 0) {
                    if (!searched) {
                        TreeNode<K,V> q, ch;
                        searched = true;
                        if (((ch = p.left) != null &&
                             (q = ch.find(h, k, kc)) != null) ||
                            ((ch = p.right) != null &&
                             (q = ch.find(h, k, kc)) != null))
                            return q;
                    }
                    dir = tieBreakOrder(k, pk);
                }

                TreeNode<K,V> xp = p;
                if ((p = (dir <= 0) ? p.left : p.right) == null) {
                    Node<K,V> xpn = xp.next;
                    TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
                    if (dir <= 0)
                        xp.left = x;
                    else
                        xp.right = x;
                    xp.next = x;
                    x.parent = x.prev = xp;
                    if (xpn != null)
                        ((TreeNode<K,V>)xpn).prev = x;
                    moveRootToFront(tab, balanceInsertion(root, x));
                    return null;
                }
            }
        }

參考文章:1.http://blog.csdn.net/fan2012huan/article/details/51097331
2.http://blog.csdn.net/crazy1235/article/details/75579654

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章