Java集合(四)HashMap详解

本文详细介绍了Java中的HashMap,包括其作为散列表的特性、线程安全性、键值对存储及遍历方式。重点分析了HashMap在JDK6和JDK8的源码,特别是容量必须为2的幂的原因、resize()机制以及putVal()函数的工作原理。此外,还讨论了HashMap的构造函数、关键函数以及遍历策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HashMap简介

java.lang.Object
   ↳     java.util.AbstractMap<K, V>
         ↳     java.util.HashMap<K, V>

public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable { }

 

HashMap是一个散列表,存储的是键值对映射。它的实现不是同步的,也就是不是线程安全的。它的key-value可以定为null。同时存储的数据是无序的。
HashMap中有两个参数影响性能:“初始容量”和“加载因子”。容量是HashMap中桶的数量。初始容量是哈希表在创建时的容量,加载因子是哈希表子在容量自动增长之前可以达到多满的一种尺度。当哈希表中的实际数量大于加载容量*当前容量时,需要对HashMap进行resize()。

HashMap源码分析

HashMap源码(JDK6)
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{

    //默认的初始容量
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    //最大容量,如果传入容量多大,将被这个值替换
    static final int MAXIMUM_CAPACITY = 1 << 30;
    //默认加载因子
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    //存储数据的Entry数组,长度是2的n次幂。
    transient Entry[] table;

    //HashMap的大小,他是HashMap保存的键值对的数量
    transient int size;

    //阀值,用于判断是否需要调整HashMap的容量,阀值=(容量*加载因子)
    int threshold;

    //加载因子的实际大小
    final float loadFactor;

    //HashMap的改变次数
    transient volatile int modCount;

    //指定容量大小和加载因子的构造函数
    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);

        //找出大于initialCapacity的最小的2的幂
        int capacity = 1;
        while (capacity < initialCapacity)
            capacity <<= 1;

        this.loadFactor = loadFactor;
        //设置阀值
        threshold = (int)(capacity * loadFactor);
        //创建Entry数组,保存数据
        table = new Entry[capacity];
        init();
    }

    //指定容量的构造函数
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    //默认构造函数
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
        init();
    }

    //含有子Map的构造函数
    public HashMap(Map<? extends K, ? extends V> m) {
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
        putAllForCreate(m);
    }
    //计算hash值(扰动函数)
    static int hash(int h) {
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

    //返回数组下标,根据hash值和HashMap的长度,计算出下标值
    static int indexFor(int h, int length) {
        return h & (length-1);
    }

    //返回HashMap的大小
    public int size() {
        return size;
    }

    //判断hashMap是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //获取key对应的vaule
    public V get(Object key) {
        if (key == null)
            return getForNullKey();
        //获取key的hash值
        int hash = hash(key.hashCode());
        //在该hash值对应的链表上查找键值等于key的元素
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                return e.value;
        }
        return null;
    }

    //获取key为null的元素的值(HashMap将key为Null的元素存储在table[0]上)
    private V getForNullKey() {
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null)
                return e.value;
        }
        return null;
    }

    //hashMap是否包含Key
    public boolean containsKey(Object key) {
        return getEntry(key) != null;
    }

    //返回键为key的键值对
    final Entry<K,V> getEntry(Object key) {
        //计算hash值,HashMap将key为Null的元素存储在table[0]的位置,key不为null的需要计算hash值
        int hash = (key == null) ? 0 : hash(key.hashCode());
        //在hash值对应的链表上查找键值=key的元素,先判断hash值是否相等,在判断key是否相等,是因为通过hash判断的效率高
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

    //将key-value添加到HashMap中
    public V put(K key, V value) {
        //如果key为Null,将键值对添加到table[0]的位置
        if (key == null)
            return putForNullKey(value);
        //key不为空,计算hash值,根据hash值获取位置索引
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        //遍历链表,判断key对应的值是否存在,存在用新值替代
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        //键值对不存在,将key-value添加到table中
        addEntry(hash, key, value, i);
        return null;
    }

    //将key为null的添加到table[0]的位置
    private V putForNullKey(V value) {
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++;
        addEntry(0, null, value, 0);
        return null;
    }

    //创建hashMap对应的添加方法,用于内部调用,被构造函数调用创建HashMap,put()用于对外提供添加元素
    private void putForCreate(K key, V value) {
        int hash = (key == null) ? 0 : hash(key.hashCode());
        int i = indexFor(hash, table.length);

        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k)))) {
                e.value = value;
                return;
            }
        }

        createEntry(hash, key, value, i);
    }

    //将m中的元素全都添加到HashMap中
    private void putAllForCreate(Map<? extends K, ? extends V> m) {
        //利用迭代器将元素逐个添加到hashMap中
        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<? extends K, ? extends V> e = i.next();
            putForCreate(e.getKey(), e.getValue());
        }
    }

    //重新调整HashMap的大小,newCapacity是调整之后的大小
    void resize(int newCapacity) {
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)(newCapacity * loadFactor);
    }

    //将HashMap中的全部元素都添加到newTable中
    void transfer(Entry[] newTable) {
        Entry[] src = table;
        int newCapacity = newTable.length;
        for (int j = 0; j < src.length; j++) {
            Entry<K,V> e = src[j];
            if (e != null) {
                src[j] = null;
                do {
                    Entry<K,V> next = e.next;
                    int i = indexFor(e.hash, newCapacity);
                    e.next = newTable[i];
                    newTable[i] = e;
                    e = next;
                } while (e != null);
            }
        }
    }
    //将m中的元素全部添加到HashMap中
    public void putAll(Map<? extends K, ? extends V> m) {
        int numKeysToBeAdded = m.size();
        if (numKeysToBeAdded == 0)
            return;  
        if (numKeysToBeAdded > threshold) {
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
            if (targetCapacity > MAXIMUM_CAPACITY)
                targetCapacity = MAXIMUM_CAPACITY;
            int newCapacity = table.length;
            while (newCapacity < targetCapacity)
                newCapacity <<= 1;
            if (newCapacity > table.length)
                resize(newCapacity);
        }
        //通过迭代将m中的元素添加到HashMap中
        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<? extends K, ? extends V> e = i.next();
            put(e.getKey(), e.getValue());
        }
    }

    //删除键为key的元素
    public V remove(Object key) {
        Entry<K,V> e = removeEntryForKey(key);
        return (e == null ? null : e.value);
    }

    //删除键为key的元素
    final Entry<K,V> removeEntryForKey(Object key) {
        int hash = (key == null) ? 0 : hash(key.hashCode());
        int i = indexFor(hash, table.length);
        Entry<K,V> prev = table[i];
        Entry<K,V> e = prev;

        while (e != null) {
            Entry<K,V> next = e.next;
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k)))) {
                modCount++;
                size--;
                if (prev == e)
                    table[i] = next;
                else
                    prev.next = next;
                e.recordRemoval(this);
                return e;
            }
            prev = e;
            e = next;
        }

        return e;
    }

    //删除键值对
    final Entry<K,V> removeMapping(Object o) {
        if (!(o instanceof Map.Entry))
            return null;

        Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
        Object key = entry.getKey();
        int hash = (key == null) ? 0 : hash(key.hashCode());
        int i = indexFor(hash, table.length);
        Entry<K,V> prev = table[i];
        Entry<K,V> e = prev;

        while (e != null) {
            Entry<K,V> next = e.next;
            if (e.hash == hash && e.equals(entry)) {
                modCount++;
                size--;
                if (prev == e)
                    table[i] = next;
                else
                    prev.next = next;
                e.recordRemoval(this);
                return e;
            }
            prev = e;
            e = next;
        }

        return e;
    }

    //清空HashMap  
    public void clear() {
        modCount++;
        Entry[] tab = table;
        for (int i = 0; i < tab.length; i++)
            tab[i] = null;
        size = 0;
    }

    //判断是否包含某值
    public boolean containsValue(Object value) {
        if (value == null)
                return containsNullValue();

        Entry[] tab = table;
            for (int i = 0; i < tab.length ; i++)
                for (Entry e = tab[i] ; e != null ; e = e.next)
                    if (value.equals(e.value))
                        return true;
        return false;
    }

    //判断是否包含null值
    private boolean containsNullValue() {
        Entry[] tab = table;
            for (int i = 0; i < tab.length ; i++)
                for (Entry e = tab[i] ; e != null ; e = e.next)
                    if (e.value == null)
                        return true;
        return false;
    }

    //克隆函数,并返回Object对象
    public Object clone() {
        HashMap<K,V> result = null;
        try {
            result = (HashMap<K,V>)super.clone();
        } catch (CloneNotSupportedException e) {
            // assert false;
        }
        result.table = new Entry[table.length];
        result.entrySet = null;
        result.modCount = 0;
        result.size = 0;
        result.init();
        result.putAllForCreate(this);

        return result;
    }

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


        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

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

        public final boolean equals(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry e = (Map.Entry)o;
            Object k1 = getKey();
            Object k2 = e.getKey();
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                Object v1 = getValue();
                Object v2 = e.getValue();
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
                    return true;
            }
            return false;
        }

        public final int hashCode() {
            return (key==null   ? 0 : key.hashCode()) ^
                   (value==null ? 0 : value.hashCode());
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }

        void recordAccess(HashMap<K,V> m) {
        }

        void recordRemoval(HashMap<K,V> m) {
        }
    }

    //将key-value添加到指定位置,一般用于新增entry可能会导致hashMap的实际容量超过阀值的情况
    void addEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
        //如果实际大小>阀值,调整HashMap的大小
        if (size++ >= threshold)
            resize(2 * table.length);
    }

    //创建entry,一般用于新增Entry不会导致hashMap的实际容量超过阀值的情况
    void createEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
        size++;
    }
    //HashIterator迭代器是HashMpa抽象出来的父类,实现了公共函数
    private abstract class HashIterator<E> implements Iterator<E> {
        Entry<K,V> next;    // next entry to return
        int expectedModCount;   // For fast-fail
        int index;      // current slot
        Entry<K,V> current; // current entry

        HashIterator() {
            expectedModCount = modCount;
            if (size > 0) { // advance to first entry
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)
                    ;
            }
        }

        public final boolean hasNext() {
            return next != null;
        }

        final Entry<K,V> nextEntry() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Entry<K,V> e = next;
            if (e == null)
                throw new NoSuchElementException();

            if ((next = e.next) == null) {
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)
                    ;
            }
            current = e;
            return e;
        }

        public void remove() {
            if (current == null)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Object k = current.key;
            current = null;
            HashMap.this.removeEntryForKey(k);
            expectedModCount = modCount;
        }
    }

    private final class ValueIterator extends HashIterator<V> {
        public V next() {
            return nextEntry().value;
        }
    }

    private final class KeyIterator extends HashIterator<K> {
        public K next() {
            return nextEntry().getKey();
        }
    }

    private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
        public Map.Entry<K,V> next() {
            return nextEntry();
        }
    }

    Iterator<K> newKeyIterator()   {
        return new KeyIterator();
    }
    Iterator<V> newValueIterator()   {
        return new ValueIterator();
    }
    Iterator<Map.Entry<K,V>> newEntryIterator()   {
        return new EntryIterator();
    }

    private transient Set<Map.Entry<K,V>> entrySet = null;


    public Set<K> keySet() {
        Set<K> ks = keySet;
        return (ks != null ? ks : (keySet = new KeySet()));
    }

    private final class KeySet extends AbstractSet<K> {
        public Iterator<K> iterator() {
            return newKeyIterator();
        }
        public int size() {
            return size;
        }
        public boolean contains(Object o) {
            return containsKey(o);
        }
        public boolean remove(Object o) {
            return HashMap.this.removeEntryForKey(o) != null;
        }
        public void clear() {
            HashMap.this.clear();
        }
    }


    public Collection<V> values() {
        Collection<V> vs = values;
        return (vs != null ? vs : (values = new Values()));
    }

    private final class Values extends AbstractCollection<V> {
        public Iterator<V> iterator() {
            return newValueIterator();
        }
        public int size() {
            return size;
        }
        public boolean contains(Object o) {
            return containsValue(o);
        }
        public void clear() {
            HashMap.this.clear();
        }
    }


    public Set<Map.Entry<K,V>> entrySet() {
        return entrySet0();
    }

    private Set<Map.Entry<K,V>> entrySet0() {
        Set<Map.Entry<K,V>> es = entrySet;
        return es != null ? es : (entrySet = new EntrySet());
    }

    private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public Iterator<Map.Entry<K,V>> iterator() {
            return newEntryIterator();
        }
        public boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<K,V> e = (Map.Entry<K,V>) o;
            Entry<K,V> candidate = getEntry(e.getKey());
            return candidate != null && candidate.equals(e);
        }
        public boolean remove(Object o) {
            return removeMapping(o) != null;
        }
        public int size() {
            return size;
        }
        public void clear() {
            HashMap.this.clear();
        }
    }


    private void writeObject(java.io.ObjectOutputStream s)
        throws IOException
    {
    Iterator<Map.Entry<K,V>> i =
        (size > 0) ? entrySet0().iterator() : null;

    // Write out the threshold, loadfactor, and any hidden stuff
    s.defaultWriteObject();

    // Write out number of buckets
    s.writeInt(table.length);

    // Write out size (number of Mappings)
    s.writeInt(size);

        // Write out keys and values (alternating)
    if (i != null) {
        while (i.hasNext()) {
        Map.Entry<K,V> e = i.next();
        s.writeObject(e.getKey());
        s.writeObject(e.getValue());
        }
        }
    }

    private static final long serialVersionUID = 362498820763181265L;


    private void readObject(java.io.ObjectInputStream s)
         throws IOException, ClassNotFoundException
    {
    // Read in the threshold, loadfactor, and any hidden stuff
    s.defaultReadObject();

    // Read in number of buckets and allocate the bucket array;
    int numBuckets = s.readInt();
    table = new Entry[numBuckets];

        init();  // Give subclass a chance to do its thing.

    // Read in size (number of Mappings)
    int size = s.readInt();

    // Read the keys and values, and put the mappings in the HashMap
    for (int i=0; i<size; i++) {
        K key = (K) s.readObject();
        V value = (V) s.readObject();
        putForCreate(key, value);
    }
    }

    // These methods are used when serializing HashSets
    int   capacity()     { return table.length; }
    float loadFactor()   { return loadFactor;   }
}

问题:
(1)为什么HashMap的容量一定要是2的n次幂? 因为我们在计算hash值的时候一般采用除留余数法计算,例如15%4=3;我们计算机在计算的时候使用&运算符更高效的实现除留余数法,也就是h%length=h&(length-1),当数组长度是2的n次幂时保证了length-1的最后一位为1,从而保证了取索引操作的h&(length-1)的最后一位同时为0和1的可能性,保证了散列的均匀性。当length为奇数时,length-1的最后一位为0,这样&操作的最后一位一定是0,那么索引位置就一定是偶数,导致数组的奇数位置全部没有放置元素,浪费空间。

HashMap源码(JDK8)

这里并没有将全部源码都分析,原因是JDK8中添加好多新的方法,这些方面没有使用过,所以也没过于关心,同时这里只罗列出了一下比较重要的方法。

类的属性
//初始容量16
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;  

//当桶(bucket)上的节点数大于这个值是会转换为红黑树
static final int TREEIFY_THRESHOLD = 8;

//当桶(bucket)上的节点数小于这个值会转换为链表
static final int UNTREEIFY_THRESHOLD = 6;

//桶中结构转换为红黑树对应的table最小大小
static final int MIN_TREEIFY_CAPACITY = 64; 
//存储元素的数组,一定是2的n次幂 
transient Node<K,V>[] table;
//存放具体的元素的集合
transient Set<Map.Entry<K,V>> entrySet;
// 存放元素的个数,注意这个不等于数组的长度
transient int size;

//改变计数
transient int modCount;
//临界值,当实际大小(容量*加载因子)超过临界值,进行扩容
int threshold;
//加载因子
final float loadFactor;
构造函数
//指定容量值和加载因子的构造函数,容量值<0抛出异常;容量值大于1 << 30,设置容量值为该值
 public HashMap(int initialCapacity, float loadFactor) {
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal initial capacity: " +
                                           initialCapacity);
    if (initialCapacity > MAXIMUM_CAPACITY)
        initialCapacity = MAXIMUM_CAPACITY;
    //加载因子小于0抛出异常
    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);
}
    //默认构造函数,没有指定容量值则为Null
    public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
//将集合中的元素添加到hashMap中
public HashMap(Map<? extends K, ? extends V> m) {
    this.loadFactor = DEFAULT_LOAD_FACTOR;
    putMapEntries(m, false);
}

针对构造函数中调用tableSizeFor,如下: 

//返回大于cap的最小二次幂值
	 static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

分析一下这个算法:

首先是cap-1,这是为了防止,cap已经是2的幂。如果cap已经是2的幂, 又没有执行这个减1操作,则执行完后面的几条无符号右移操作之后,返回的capacity将是这个cap的2倍

第一次右移: n |= n >>> 1;因为n不等于0,那么在二进制中总会有一个bit是1,这时考虑最高位的1,通过无符号右移一位,那么最高位的1右移1位,再做或操作,使得n中的二进制中的与最高位的1紧邻的右边一位也是1.

第二次右移n |= n >>> 2;同理最高位的1右移了2位,然后与原值或,这样二进制中的最高位中会有4个连续的1。

第三次右移:n |= n >>> 4;同理最高位的1右移了4为,然后与原值或,这样二进制中的最高位会有8个连续的1.

以此类推。。。。
经过上面的我们看到,容量最大就是32位都为1,已经大于了我们的最大值,所以取最大值。经过上面的推算,我们可以看到,给定一个数,经过上面的右移操作,最终会对这个数据没有什么操作  
针对于构造函数中调用putMapEntries()说明:

final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
    int s = m.size();
    if (s > 0) {
        //判断table是否已经初始化,没有初始化,s就是实际元素
        if (table == null) { // pre-size
            float ft = ((float)s / loadFactor) + 1.0F;
            int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                     (int)ft : MAXIMUM_CAPACITY);
            // 计算得到的t大于阈值,则初始化阈值
            if (t > threshold)
                threshold = tableSizeFor(t);
        }
         // 已初始化,并且m元素个数大于阈值,进行扩容处理
        else if (s > threshold)
            resize();
        // 将m中的所有元素添加至HashMap中
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
            K key = e.getKey();
            V value = e.getValue();
            putVal(hash(key), key, value, false, evict);
        }
    }
}
重要函数

(1)putVal函数,同时hashmap中有一个put函数,也是调用了putVal

 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是否为空,为空或者长度=0,进行扩容;table不为空,根据计算出来的hash插入对应的数组索引
    if ((tab = table) == null || (n = tab.length) == 0)
        n = (tab = resize()).length;
    //根据计算出的hash确定元素放到哪个桶中,桶为空,新生成节点放入桶中
    if ((p = tab[i = (n - 1) & hash]) == null)
        tab[i] = newNode(hash, key, value, null);
    else {
        //桶中已经存在元素
        Node<K,V> e; K k;
        //比较桶中的元素的key是否存在,存在直接覆盖value
        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);
                    //判断结束数量是否达到8,转换为红黑树
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);
                    break;
                }
                // 判断链表中结点的key值与插入的元素的key值是否相等,
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        // 表示在桶中找到key值、hash值与插入元素相等的结点,覆盖旧值
        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;
}

让我们梳理一下putVal()的逻辑: 
(2)resize()函数

final Node<K,V>[] resize() {
    //保存当前table
    Node<K,V>[] oldTab = table;
    //table大小
    int oldCap = (oldTab == null) ? 0 : oldTab.length;
    //保存当前阀值
    int oldThr = threshold;
    int newCap, newThr = 0;
    //之前table大于0
    if (oldCap > 0) {
        //之前table大于最大值,阀值为最大整形,并返回
        if (oldCap >= MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return oldTab;
        }
        //之前table没有达到最大值,容量翻倍,左移效率高
        else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                 oldCap >= DEFAULT_INITIAL_CAPACITY)
            newThr = oldThr << 1; // double threshold
    }
    //之前阀值大于0,新容量=原阀值
    else if (oldThr > 0) // initial capacity was placed in threshold
        newCap = oldThr;
    else { 
        //新容量为默认16,新阀值为16*0.75
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
    //新的阀值==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<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
    table = newTab;
    //之前table已经初始化过
    if (oldTab != null) {
        //遍历原table
        for (int j = 0; j < oldCap; ++j) {
            Node<K,V> e;//e指向oldTab数组中的元素,即每个链表中的头结点
            if ((e = oldTab[j]) != null) {
                oldTab[j] = null;
                if (e.next == null)//链表中只有一个头结点
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)//判断是否是红黑树节点,是的话,调用split调整
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                else { // preserve order
                    //是链表,对链表进行秩序维护,因为我们使用了2倍扩容,所以桶中的元素必须是要么待在原来索引的对应位置,要么在新的桶中的位置便宜2倍
                    Node<K,V> loHead = null, loTail = null;
                    Node<K,V> hiHead = null, hiTail = null;
                    Node<K,V> next;
                    do {
                        next = e.next;
                        // 将同一桶中的元素根据(e.hash & oldCap)是否为0进行分割,分成两个不同的链表,完成rehash
                        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;
}

梳理一下,做了什么优化:
我们在扩容的时候使用的是2的次幂的扩展,所以元素的位置要么在原位置要么在原位置再一定2次幂的位置,如图,原容量为16,那么n-1 = 0000 1111,扩大2次幂就是 0001 1111.图(a)表示扩容前的key1和key2两种key确定索引位置的示例,图(b)表示扩容后key1和key2两种key确定索引位置的示例,其中hash1是key1对应的哈希与高位运算结果.

元素在重新计算hash之后,因为n变为2倍,那么n-1的mask范围在高位多1bit(红色),因此新的index就会发生这样的变化。
因此,我们在扩充HashMap的时候,不需要像JDK1.7的实现那样重新计算hash,只需要看看原来的hash值新增的那个bit是1还是0就好了,是0的话索引没变,是1的话索引变成“原索引+oldCap”,可以看看下图为16扩充为32的resize示意图:

(3)getNode函数,HashMap中的get调用给用户的,就是通过调用getNode函数

final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    //先判断是否是红黑树,如果是红黑树,直接从红黑树中拿取,如果不是从链表中获取数据
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (first = tab[(n - 1) & hash]) != null) {
        if (first.hash == hash && // always check first node
            ((k = first.key) == key || (key != null && key.equals(k))))
            return first;
        if ((e = first.next) != null) {
            if (first instanceof TreeNode)
                return ((TreeNode<K,V>)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;
}

(4)removeNode()函数

 final Node<K,V> removeNode(int hash, Object key, Object value,
                           boolean matchValue, boolean movable) {
    Node<K,V>[] tab; Node<K,V> p; int n, index;
    //table数组非空,键的hash值所指向的数组中的元素非空
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (p = tab[index = (n - 1) & hash]) != null) {
        Node<K,V> node = null, e; K k; V v;//node指向最终的结果结点,e为链表中的遍历指针
        if (p.hash == hash &&  //检查第一个节点,如果匹配成功
            ((k = p.key) == key || (key != null && key.equals(k))))
            node = p;
         //如果第一个节点匹配不成功,则向后遍历链表查找
        else if ((e = p.next) != null) {
            if (p instanceof TreeNode)
                node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
            else {
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key ||
                         (key != null && key.equals(k)))) {
                        node = e;
                        break;
                    }
                    p = e;
                } while ((e = e.next) != null);
            }
        }
        if (node != null && (!matchValue || (v = node.value) == value ||
                             (value != null && value.equals(v)))) {
            if (node instanceof TreeNode)
                ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
            else if (node == p)
                tab[index] = node.next;
            else
                p.next = node.next;
            ++modCount;
            --size;
            afterNodeRemoval(node);
            return node;
        }
    }
    return null;
}

HashMap遍历

1.entrySet获取键值对,再通过Iterator遍历

    Iterator iterator = hashMap.entrySet().iterator();
    while (iterator.hasNext()){
        Map.Entry entity = ( Map.Entry) iterator.next();
        System.out.println("key="+entity.getKey()+",value="+entity.getValue());
    }  

2.通过keySet获取键的set集合,再通过Iterator遍历键集合

    Iterator iterator1 = hashMap.keySet().iterator();
    while (iterator1.hasNext()){
        String key = iterator1.next().toString();
        System.out.println("key="+key+",value="+hashMap.get(key));
    }

HashMap问题:

(1)说明一下tableSizeFor是如何保证拿到大于等于容量的最小的2的次幂的? 首先我们将cap-1,然后得到的值在右移一位,这样就保证最高位的1右移1位,同时跟原来的值或操作,那么就保证最高位旁边有两个连续的1。同理右移2位,就有4个1,右移4为8个一,这样最大的值就是2的32次幂。如果我们传入任意一个数,在操作的过程中,会右移然后或操作,导致数不变,那么此时就会得到大于等于容量的最小的2次幂

(2)描述一下java8中HashMap是如何进行put? 
我们在put值的时候,会调用putVal函数,先判断table是否为空(table的长度是否为0),如果为空,先进行扩容;如果不为空根据键Key计算hash值,得到数组索引。判断table[i]是否为空。为空直接插入;不为空先判断key是否存在,存在直接覆盖;不存在先判断该节点是不是红黑树,是红黑树直接插入键值对;不是红黑树开始遍历链表插入数据,判断链表长度是否大于8,是的话转换为红黑树,不是的链表插入,若Key存在覆盖。最后判断容量值是否需要扩容。 (3)描述一下java8中的resize()的实现方式?巧妙之处在于哪里? 
jdk7中在扩容的时候使用的是hash值与数组长度-1这个掩码进行与运算,得到新的Entry元素的新下标位置.
jdk8中扩容的时候使用的hash值与数组长度进行与运算,因为数组长度是2次幂的长度,所以得到的是0或者非0,0表示新位置不变,是1索引变成“原索引+原容量”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值