HashMap源码解析

HashMap源码解析:

hash函数

JDK1.8hash函数:

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

JDK1.7hash函数

    final int hash(int h) {

        h ^= k.hashCode();

        h ^= (h >>> 20) ^ (h >>> 12);//>>>是无符号右移,忽略符号位
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

在.17中,进行了4次扰动,为什么要进行扰动?

因为hashmap,计算出hash值,插入的点为  (n-1)&hash,如果不进行扰动,如果n也较小,那么低位相同,高位不同的hash最后结果会全部相同.所有需要进行扰动,jdk1.7中扰动4次,jdk1.8中扰动1次,扰动次数少速度快一些.

在hashmap中,初始长度为n,n一定是2的幂次方,只有n为2的幂次方,(n-1)&hash =  hash%n,在计算机中&运算比%运算快的多.


类属性

public class HashMap extends AbstractMap
    implements Map, Cloneable, Serializable {

    private static final long serialVersionUID = 362498820763181265L;//序列号
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 初始容量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表容量>=64才运行转换为红黑树

    transient Node[] table;                        //hash表数组
    transient Set> entrySet;             //存放具体元素的集
    transient int size;                                 //hashmap中的元素数目
    transient int modCount;                             //扩容更改map结构的计数器
    int threshold;                                      //临界值,(容量*填充因子)>临界值就扩容
    final float loadFactor;                             //加载因子

loadFactor加载因子

加载因子hashmap默认为0.75f.越接近1,hash表数组被用的越多,越加密集,越密,插入的时候越容易让链表长度增加.

加载因子不能太大,太大就很容易碰撞,太小数组利用率低,也占空间.

threshold临界值

threshold=capacity*loadFactor,如果hashmap元素数目>临界值,那么就需要扩容,也就是增大hash数组长度,然后重新分配元素.除非已经达到MAXIMUM_CAPACITY.


Node节点

    static class Node implements Map.Entry {
        final int hash;
        final K key;
        V value;
        Node next;

        Node(int hash, K key, V value, Node next) {//构造Node
            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() {//重写了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) {//重写了equals
            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;
        }
    }

Node包含4个元素:

  • hash值
  • key值
  • value值
  • next,指向下一个节点

构造方法

有四种构造方法,可以自定义初始hash数组长度,和加载因子.也可以构造的时候,就包含另一个map.

设置的hash长度,会向上取到 2的幂次大小.

    public HashMap(int initialCapacity, float loadFactor) {//自行设置容量和加载因子
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }
    public HashMap(int initialCapacity) {//设置容量
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
    public HashMap() {//默认
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
    public HashMap(Map m) {//包含另一个map
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }

由于我们插入的元素可能非常多,可能进行多次扩容,而扩容又是很消耗时间的,所有如果我们确定会插入很多元素,那么可以构造的时候指定hashmap的hash数组长度,避免频繁扩容.

putMapEntries

    final void putMapEntries(Map m, boolean evict) {
        int s = m.size();
        if (s > 0) {//table已经初始化
            if (table == null) { // pre-size  hash数组还没初始化,那么就初始化,下面计算threshold是否会大于阀值,如果大于就初始化阀值
                float ft = ((float)s / loadFactor) + 1.0F;
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                         (int)ft : MAXIMUM_CAPACITY);
                if (t > threshold)
                    threshold = tableSizeFor(t);
            }
            else if (s > threshold)//已经初始化了,那么扩容
                resize();
            for (Map.Entry e : m.entrySet()) {//将m的元素插入到新hashMap中
                K key = e.getKey();
                V value = e.getValue();
                putVal(hash(key), key, value, false, evict);
            }
        }
    }

get方法

    public V get(Object key) {
        Node e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    final Node getNode(int hash, Object key) {
        Node[] tab; Node first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {//包含key,也就是找到的hash数组非空
            if (first.hash == hash && // always check first node   头节点的key就是要取的key
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {//在链表里/树上
                if (first instanceof TreeNode)//头节点是树节点,调用树查找
                    return ((TreeNode)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);//在链表里查找
            }
        }
        return null;
    }

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[] tab; Node p; int n, i;//tab是hash表数组,p用于记录链表节点,n是hash数组长度,i是一个索引值
        if ((tab = table) == null || (n = tab.length) == 0)//hash表为空,就扩容
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)//通过(n-1)&hash找到目标hash坐标,发现空的就直接插入
            tab[i] = newNode(hash, key, value, null);
        else {//否则需要插入链表中/红黑树中
            Node e; K k;//e是新增的节点,k是保存key的
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;//将第一个元素赋值给e,用e来记录
            else if (p instanceof TreeNode)//是树节点,插入红黑树中
                e = ((TreeNode)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))))//找到key相同的节点,跳出
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key  已存在key,覆盖value
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;//记录hashmap更改次数的计数器+1
        if (++size > threshold)//需要hash数组扩容
            resize();
        afterNodeInsertion(evict);
        return null;
    }
  • 如果定位到的hash数组空的,那么直接插入元素
  • 如果定位到的hash数组非空,就和第一个节点比较,如果相同就覆盖,如果是树节点那么久插入到树中,否则插入到链表(尾插)中,可能在链表中已经有需要插入的key,那么就覆盖.如果插入链表后链表长度>8,呢么就将链表转换为红黑树.

插入完了后如果size>阀值,就扩容

删除节点和插入是差不多的.


resize扩容

    final Node[] resize() {
        Node[] oldTab = table;//保存原来的hash数组
        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;   //临界值设置为int极限大
                return oldTab;
            }
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold  扩容为原来2倍
        }
        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);//oldThr==0,使用默认值
        }
        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[] newTab = (Node[])new Node[newCap];
        table = newTab;
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {//遍历数组,将老数组的桶迁移到新的hash数组中
                Node 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)e).split(this, newTab, j, oldCap);//红黑树特殊处理
                    else { // preserve order           
                        Node loHead = null, loTail = null;
                        Node hiHead = null, hiTail = null;
                        Node 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;
    }

扩容操作,需要重新分配hash表上所有的元素,非常消耗时间,我们在用hashmap的时候应该避免频繁扩容

你可能感兴趣的:(Java学习,hashmap)