Java Hashmap底层原理

静态属性

    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;

节点(解决冲突:拉链法处理)

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) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = 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;
        }
    }

    transient Node<K,V>[] table;

这里写图片描述
参考

Put方法

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

HashMap 包含如下几个构造器:

HashMap()
//构建一个初始容量为 16,负载因子为 0.75 的 HashMap。

HashMap(int initialCapacity)
//构建一个初始容量为 initialCapacity,负载因子为 0.75的HashMap。 

HashMap(int initialCapacity, float loadFactor)
//以指定初始容量、指定的负载因子创建一个HashMap。

HashMap的基础构造器HashMap(int initialCapacity, float loadFactor)
//带有两个参数,它们是初始容量initialCapacity和加载因子loadFactor。

initialCapacity:HashMap的最大容量,即为底层数组的长度
loadFactor:负载因子loadFactor定义为:散列表的实际元素数目(n)/ 散列表的容量(m)

负载因子衡量的是一个散列表的空间的使用程度,负载因子越大表示散列表的装填程度越高,反之愈小。对于使用链表法的散列表来说,查找一个元素的平均时间是O(1+a),因此如果负载因子越大,对空间的利用更充分,然而后果是查找效率的降低;如果负载因子太小,那么散列表的数据将过于稀疏,对空间造成严重浪费。

threshold = (int)(capacity * loadFactor);  

if (size++ >= threshold)   
    resize(2 * table.length); 

threshold就是在此loadFactor和capacity对应下允许的最大元素数目,超过这个数目就重新resize,以降低实际的负载因子(也就是说虽然数组长度是capacity,但其扩容的临界值确是threshold)默认的的负载因子0.75是对空间和时间效率的一个平衡选择。当容量超出此最大容量时, resize后的HashMap容量是容量的两倍。

Fail-Fast机制

如果在使用迭代器的过程中有其他线程修改了map,那么将抛出ConcurrentModificationException,这就是所谓fail-fast策略

//modCount顾名思义就是修改次数,对HashMap内容的修改都将增加这个值,那么在迭代器初始化过程中会将这个值赋给迭代器的expectedModCount
HashIterator() {
    expectedModCount = modCount;
    if (size > 0) { // advance to first entry
    Entry[] t = table;
    while (index < t.length && (next = t[index++]) == null)
        ;
    }
}

//注意到modCount声明为volatile,保证线程之间修改的可见性。(volatile之所以线程安全是因为被volatile修饰的变量不保存缓存,直接在内存中修改,因此能够保证线程之间修改的可见性)
final Entry<K,V> nextEntry() {   
    if (modCount != expectedModCount)   
        throw new ConcurrentModificationException();

volatile变量本身并非并发安全的变量
迭代器的快速失败行为不能得到保证,一般来说,存在非同步的并发修改时,不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的做法是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测程序错误。

HashTable

参考源码

和HashMap的主要区别:
加上了synchronized的同步代码块的机制

 public synchronized V get(Object key) {
      Entry tab[] = table;
      int hash = key.hashCode();
      // 计算索引值,
      int index = (hash & 0x7FFFFFFF) % tab.length;
      // 找到“key对应的Entry(链表)”,然后在链表中找出“哈希值”和“键值”与key都相等的元素
      for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
          if ((e.hash == hash) && e.key.equals(key)) {
             return e.value;
         }
     }
     return null;
}

public synchronized V put(K key, V value) {
     // Hashtable中不能插入value为null的元素!!!
     if (value == null) {
         throw new NullPointerException();
     }

     // 若“Hashtable中已存在键为key的键值对”,
     // 则用“新的value”替换“旧的value”
     Entry tab[] = table;
     int hash = key.hashCode();
     int index = (hash & 0x7FFFFFFF) % tab.length;
     for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
         if ((e.hash == hash) && e.key.equals(key)) {
             V old = e.value;
             e.value = value;
             return old;
             }
     }

     // 若“Hashtable中不存在键为key的键值对”,
     // (01) 将“修改统计数”+1
     modCount++;
     // (02) 若“Hashtable实际容量” > “阈值”(阈值=总的容量 * 加载因子)
     //  则调整Hashtable的大小
     if (count >= threshold) {
         // Rehash the table if the threshold is exceeded
         rehash();

         tab = table;
         index = (hash & 0x7FFFFFFF) % tab.length;
     }

     // (03) 将“Hashtable中index”位置的Entry(链表)保存到e中
     Entry<K,V> e = tab[index];
     // (04) 创建“新的Entry节点”,并将“新的Entry”插入“Hashtable的index位置”,并设置e为“新的Entry”的下一个元素(即“新Entry”为链表表头)。        
     tab[index] = new Entry<K,V>(hash, key, value, e);
     // (05) 将“Hashtable的实际容量”+1
     count++;
     return null;
 }

扩容机制

     @Override  
    public V compute(K key,  
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {  
        if (remappingFunction == null)  
            throw new NullPointerException();  
        int hash = hash(key);  
        Node<K,V>[] tab; Node<K,V> first; int n, i;  
        int binCount = 0;  
        TreeNode<K,V> t = null;  
        Node<K,V> old = null;  
        if (size > threshold || (tab = table) == null ||  
            (n = tab.length) == 0)  
            n = (tab = resize()).length;  
        if ((first = tab[i = (n - 1) & hash]) != null) {  
            if (first instanceof TreeNode)  
                old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);  
            else {  
                Node<K,V> e = first; K k; 
                //遍历得到长度 
                do {  
                    if (e.hash == hash &&  
                        ((k = e.key) == key || (key != null && key.equals(k)))) {  
                        old = e;  
                        break;  
                    }  
                    ++binCount;  
                } while ((e = e.next) != null);  
            }  
        }  
        V oldValue = (old == null) ? null : old.value;  
        V v = remappingFunction.apply(key, oldValue);  
        if (old != null) {  
            if (v != null) {  
                old.value = v;  
                afterNodeAccess(old);  
            }  
            else  
                removeNode(hash, key, null, false, true);  
        }  
        else if (v != null) {  
            if (t != null)  
                t.putTreeVal(this, tab, hash, key, v);  
            else {  
                tab[i] = newNode(hash, key, v, first);  
                if (binCount >= TREEIFY_THRESHOLD - 1) 
                //如果长度超过阈值,则将链表转为红黑树,阈值长度默认为8 
                    treeifyBin(tab, hash);  
            }  
            ++modCount;  
            ++size;  
            afterNodeInsertion(true);  
        }  
        return v;  
    } 

     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)     //如果table数组尚未创建(第一次调用put),则新建table数组  
            n = (tab = resize()).length;  
        if ((p = tab[i = (n - 1) & hash]) == null)              //table[i]中没有结点则创建新节点  
            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)))) //如果p=table[i]的关键字与给定关键字key相同,则替换旧值  
                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);  
                        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) {            // e不为空,即map中存在要添加的关键字  
                V oldValue = e.value;  
                if (!onlyIfAbsent || oldValue == null)  
                    e.value = value;  
                afterNodeAccess(e);  
                return oldValue;  
            }  
        }  
        ++modCount;                 //修改map结构的操作数加1  
        if (++size > threshold)      //如果超出重构阈值,需要重新分配空间  
            resize();  
        afterNodeInsertion(evict);  
        return null;  
    }  

位桶+链表/红黑树
JDK7:链表+纵向链表位桶->导致冲突高时,每次查询都是顺序遍历,效率差
JDK8:JDK7的基础上,当位桶链表大于某个长度,将链表转为红黑树,查询效率为logn

TreeMap直接实现就是红黑树

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值