java-源码篇-HashMap

摘要

java 8 对 HashMap 底层的实现进行了优化,如引入红黑树的数据结构和扩容的优化。

存储结构

从结构实现来讲,HashMap 是数组 + 链表 + 红黑树(java 8 增加了红黑树部分)实现的。链表长度大于 8 时转换为红黑树。

HashMap 中字段 Node<K,V>[] table,即哈希桶数组。

// HashMap 内部类 Node
static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;// 定位数组索引位置
        final K key;
        V value;
        Node<K,V> next;// 链表的下一个节点

        Node(int hash, K key, V value, Node<K,V> next) { ... }
        public final K getKey() { return key; }
        public final V getValue() { return value; }
        public final String toString() { return key + "=" + value; }
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
}

HashMap 就是使用哈希表来存储的。哈希表为解决冲突,可以采用开放寻址法和链地址法等来解决问题,java 中 HashMap 采用了链地址法

通过什么方式来控制 map 使得 Hash 碰撞的概率又小,哈希桶数组占用空间又少呢?
好的 Hash 算法和扩容机制。

即使负载因子和 Hash 算法设计的再合理,也免不了会出现拉链过长的情况?
JDK 1.8 中,对数据结构做了进一步的优化,引入了红黑树。链表长度过长时,链表就转换为红黑树,利用红黑树快速增删改查的特点提高 HashMap 的性能。

put 方法

java 8 HashMap 的 put 方法源码如下:

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

final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        // table是否为空或 length==0,是的话 resize() 进行扩容
        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)
                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;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
}

扩容机制

猜你喜欢

转载自blog.csdn.net/u010019244/article/details/105918107
今日推荐