首页 > 编程语言 >JDK 1.8 HashMap的源码分析

JDK 1.8 HashMap的源码分析

时间:2023-01-18 17:12:11浏览次数:41  
标签:hash HashMap int 1.8 value 源码 key null final

    /**
     * HashMap的特点:
     * 1. AbstractMap Map 冗余
     * 2. 与hashTable一样 1.1 效率低,线程安全,key 不为null  hashMap1.2  效率高,key为null
     * 3. 按照key进行存放
     * 4. 必须重写hascode equal两个方法
     * 5. LinkHashMap 输入有序 加入一个链表
     */
public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    //默认的数组初始长度16
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * 默认数组的最大值
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * 默认加载因子0.75 折中(0.5-1)取0.5 空间浪费 1哈希冲突太多,所以折中.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    
    //存放位置的链表
    transient Node<K,V>[] table;
    //集合大小
    transient int size;


    /**
     * 重设大小的门槛值
     */
    int threshold;

    /**
     * 加载因子,初始被设置0.75
     */
    final float loadFactor;
    
    /**
     * 存放的是链表中的一个节点
     **/
     static class Node<K,V> implements Map.Entry<K,V> {
    
        final int hash; //hash值
        final K key; // key值
        V value;//key对应的value
        Node<K,V> next;//下一个节点
        }
    /**
     * 计算hash值
     **/
            static final int hash(Object key) {
        int h;
        //二次散列,增加hash的命中率,避免hash冲突
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    /**
     * 添加元素
     **/
        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 为空
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        //(n - 1) & hash 求它在数组中的位置 hash对长度取余
        //在现有的table中是否为空,如果为null直接添加,否则存在hash冲突
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
            //hash冲突的解决方案 直接覆盖值,使用之前的key
        else {
            Node<K,V> e; K k;
            //hash相等 且key相等
            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)
                //覆盖之前的值,而没有更新key。所以key是之前的key。返回的是之前的值
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
}
   

标签:hash,HashMap,int,1.8,value,源码,key,null,final
From: https://www.cnblogs.com/youran-he/p/17060231.html

相关文章