HashMap实现原理

结构:数组+链表+红黑树

1.HashMap可以添加null类型的键值,并且可以保证正常的输出。(HashTable虽然添加是编译器不报错,但是获取Null的键时会报空指针异常)。

2.两个决定HashMap性能的重要参数:
initial capacity:初始容量,默认16。
load factor:负载因子,默认0.75.
负载因子(已经使用/总长度)决定当数组扩充的时机,这个0.75是均衡查找时间和空间占用所得的一个数值。如果比较小,比如0.5则数组使用了一半就开始扩容,则空间浪费;如果比较大,比如0.9则每一个数组内存放的元素又太多了,那么查找时间会比较长。

3.HashMap是线程不安全的。如果要保证并发情况下正确使用,那么可以使用 Map m = Collections.synchronizedMap(new HashMap(…));

4.迭代是快速失败的fail-fast。如果在迭代过程中HashMap结构发生变化,将抛出ConcurrentModificationException异常,只有使用迭代器本身的remove方法才不会出错,只能用来检查错误,他不能代替同步。
使用实例


变量:


数组默认初始容量:
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;
链表转化为红黑树的阀值:
static final int TREEIFY_THRESHOLD = 8;
红黑树转化为链表的阀值:
static final int UNTREEIFY_THRESHOLD = 6;
树的最小容量:有一个扩容条件用到次参数
static final int MIN_TREEIFY_CAPACITY = 64;

hash算法:

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

映射到数组下标:tab[i = (n - 1) & hash])

扩容时的拆分:e.hash & oldCap

[参考链接](https://www.cnblogs.com/liujinhong/p/6576543.html)

插入:

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        第一次插入时扩容
        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;相同的key则替换
            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) {
                        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;
                访问的回调操作LinkedHashMap实现了
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            如果数组使用达到阀值,扩容
            resize();
         插入的回调操作
        afterNodeInsertion(evict);
        return null;
    }

扩容+转换:挺重要扩容中hash参考

    final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        int oldThr = threshold;
        int newCap, newThr = 0;
        if (oldCap > 0) {
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            扩容2倍:16-32
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                 阀值两倍:12-24
                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;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        //第一次插入时:阀值12
        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;
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {
                    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
    链表的转移:e.hash & oldCap 根据第X位(扩容后幂数位),将链表分成两部分,一部分还是占据原来的数组位置,另一部分转移到 当前+原来数组长度的位置。
                        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;
    }

将链表转换为树的方法:

final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        这里还有一个扩容判断,表的长度必须要大于64才扩容。也就是说当一个链表的长度大于8,但是数组长度还不到64那么应该先扩容,而不是先转化为树,因为树是比较占空间。
        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);
        }
    }

扩容时树的转换:多了树转链表的判断。拆分后两颗树的节点是不相同的,如果数量比较少就没必要作为树了。

final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
            TreeNode<K,V> b = this;
            // Relink into lo and hi lists, preserving order
            TreeNode<K,V> loHead = null, loTail = null;
            TreeNode<K,V> hiHead = null, hiTail = null;
            int lc = 0, hc = 0;
            for (TreeNode<K,V> e = b, next; e != null; e = next) {
                next = (TreeNode<K,V>)e.next;
                e.next = null;
                if ((e.hash & bit) == 0) {
                    if ((e.prev = loTail) == null)
                        loHead = e;
                    else
                        loTail.next = e;
                    loTail = e;
                    ++lc;
                }
                else {
                    if ((e.prev = hiTail) == null)
                        hiHead = e;
                    else
                        hiTail.next = e;
                    hiTail = e;
                    ++hc;
                }
            }

            if (loHead != null) {
                if (lc <= UNTREEIFY_THRESHOLD)
                    tab[index] = loHead.untreeify(map);
                else {
                    tab[index] = loHead;
                    if (hiHead != null) // (else is already treeified)
                        loHead.treeify(tab);
                }
            }
            if (hiHead != null) {
                if (hc <= UNTREEIFY_THRESHOLD)
                    tab[index + bit] = hiHead.untreeify(map);
                else {
                    tab[index + bit] = hiHead;
                    if (loHead != null)
                        hiHead.treeify(tab);
                }
            }
        }

总结:除了理解Hash表的扩容时机,转换过程,很有必要理解几个hash处理过程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值