HashMap,你知道多少?

1、前言

HashMap在面试中是个火热的话题,那么你能应付自如吗?下面抛出几个问题看你是否知道,若是知道那么本文对于你来讲就不值一提了。
  • HashMap的内部数据结构是什么?
  • HashMap扩容机制时什么?何时扩容?
  • HashMap其长度有什么特征?为何是这样?
  • HashMap为何线程不安全?并发的场景会出现什么的状况?
本文是基于JDK1.7.0_79版本进行研究的。

2、源码解读

一、类的继承关系

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
其中继承了AbstractMap抽象类,别小看了这个抽象类哦,它实现了Map接口的许多重要方法,大大减小了实现此接口的工做量。

二、属性解析

2.一、capacity:容量

  • DEFAULT_INITIAL_CAPACITY:默认的初始容量-必须是2的幂。为何呢?先留个疑问在这
/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
  • MAXIMUM_CAPACITY:最大容量为2^30。

2.2 threshold:阈值

/**
 * The next size value at which to resize (capacity * load factor).
 * @serial
 */
// If table == EMPTY_TABLE then this is the initial capacity at which the
// table will be created when inflated.
int threshold;
从上面注释能够看出, 它的值是由容量和加载因子决定的。

2.3 loadFactor:加载因子,默认为0.75

/**
 * The load factor used when none specified in constructor.
 */
static final float DEFAULT_LOAD_FACTOR = 0.75f;

2.4 size:键值对长度

/**
 * The number of key-value mappings contained in this map.
 */
transient int size;

2.5 modCount:修改内部结构的次数

transient int modCount;
上面五个属性字段都很重要, 后面再分析体现其重要。
 

三、底层数据结构

static final Entry<?,?>[] EMPTY_TABLE = {};

/**
 * The table, resized as necessary. Length MUST Always be a power of two.
 * 这里也强调扩容时,长度必须是2的指数次幂
 */
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
Entry内部结构以下:
static class Entry<K,V> implements Map.Entry<K,V> {
    final K key;
    V value;
    Entry<K,V> next;
    int hash;
}
经分析后其数据结构为数组+链表的形式,展现图以下:

四、重要函数

4.1 构造函数

总共有四个构造函数, 主要分析含有两个参数的构造函数:
其实这个构造函数也主要是初始化加载因子和阈值。(可能1.7的其余版本会有点不同,会在构造函数中初始化table)
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;
    threshold = initialCapacity;
    // 供子类实现
    init();
}
 

4.2 put()函数

public V put(K key, V value) {
    // 1 若是table为空则须要初始化
    if (table == EMPTY_TABLE) {
        inflateTable(threshold);
    }
    // 2 若是key为空,则单独处理
    if (key == null)
        return putForNullKey(value);
     // 3 根据key获取hash值   
    int hash = hash(key);
    // 4 根据hash值和长度求取索引值。
    int i = indexFor(hash, table.length);
    // 5 根据索引值获取数组下的链表进行遍历,判断元素是否存在相同的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;
        }
    }

    // 6 若是不存在重复的key, 则须要建立新的Entry,而后添加至链表中。
    // 先将修改次数加一
    modCount++;
    addEntry(hash, key, value, i);
    return null;
}
  • 第一步:当table尚未初始化时,看下inflateTable()函数作了什么操做。
private void inflateTable(int toSize) {
    // Find a power of 2 >= toSize
    int capacity = roundUpToPowerOf2(toSize);

    // 其中阈值=容量*加载因子,而后再初始化数组。
    threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
    table = new Entry[capacity];
    initHashSeedAsNeeded(capacity);
}
  • 其中容量是根据toSize取第一个大于它的2的指数次幂的值, 以下,其中highestOneBit函数是返回其最高位的权值,用的最巧的就是(number - 1) << 1 其实就是取number的倍数, 但综合使用却能取得第一个大于等于该值的2的指数次幂。(用的牛逼)
private static int roundUpToPowerOf2(int number) {
    // assert number >= 0 : "number must be non-negative";
    return number >= MAXIMUM_CAPACITY
            ? MAXIMUM_CAPACITY
            : (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
  • 接着看put函数的第二步:当key为null时,会取数组下标为0的位置进行链表遍历,若是存在key=null,则替换值并返回。不然进入第六步(注意:索引值依然指定是0)。
private V putForNullKey(V value) {
    // 取数组下标为0的链表
    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++;
    // 注意:索引值依然指定是0
    addEntry(0, null, value, 0);
    return null;
}
  • 第三步:根据key的hashCode求取hash值,这又是个神奇的算法,这里不作多解释。
final int hash(Object k) {
    int h = hashSeed;
    if (0 != h && k instanceof String) {
        return sun.misc.Hashing.stringHash32((String) k);
    }
    
    h ^= k.hashCode();

    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);
}
  • 第四步:根据hash值和底层数组的长度计算索引下标。由于数组的长度是2的幂,因此h & (length-1)运算其实就是h与(length-1)的取模运算。不得不服啊,将计算运用的如此高效。
static int indexFor(int h, int length) {
    // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
    return h & (length-1);
}
找个数验证下:
  • 第五步是验证是否有重复key,若是有则替换新值而后返回,源码很详细了就再也不作解释了。
  • 第六步:是将值添加到entry数组中,详细看下addEntry()函数。首先根据size和阈值判断是否须要扩容(进行两倍扩容),若是须要扩容则先扩容从新计算索引,则建立新的元素添加至数组
void addEntry(int hash, K key, V value, int bucketIndex) {
    // 若是长度大于阈值,则须要进行扩容
    if ((size >= threshold) && (null != table[bucketIndex])) {
        // 进行2倍扩容
        resize(2 * table.length);
        hash = (null != key) ? hash(key) : 0;
        // 扩容以后由于长度变化了,须要从新计算下索引值。
        bucketIndex = indexFor(hash, table.length);
    }
    // 而后进行添加元素
    createEntry(hash, key, value, bucketIndex);
}

void createEntry(int hash, K key, V value, int bucketIndex) {
    Entry<K,V> e = table[bucketIndex];
    // 往表头插入
    table[bucketIndex] = new Entry<>(hash, key, value, e);
    size++;
}
其中扩容机制resize()函数须要重点捞出来晒下: newCapacity = 2 * length,理论上会进行两倍扩容但会根最大容量进行对比取最小, 建立新数组而后将就数组中的值拷贝至新数组(其中会从新计算索引下标),而后再赋值给table, 最后再从新计算阈值。
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, initHashSeedAsNeeded(newCapacity));
    table = newTable;
    // 从新计算阈值
    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
  接着看transfer()函数,多注意这个函数中循环的内容
void transfer(Entry[] newTable, boolean rehash) {
    int newCapacity = newTable.length;
    for (Entry<K,V> e : table) {
        while(null != e) {
            // 定一个next
            Entry<K,V> next = e.next;
            if (rehash) {
                e.hash = null == e.key ? 0 : hash(e.key);
            }
            // 从新计算索引下标。
            int i = indexFor(e.hash, newCapacity);
            // 头插法,
            e.next = newTable[i];
            newTable[i] = e;
            // 接着下个节点继续遍历
            e = next;
        }
    }
}

  经过上面分析,其实put函数仍是简单的,不是很绕。那么能从其中找到开头的第二和第三个问题的答案吗?下面总结下顺便回答下这两个问题:javascript

一、数组长度不论是初始化仍是扩容时,都始终保持是2的指数次幂。为何呢?下面个人分析:
  • 能使元素均匀分布,增大空间利用率。put值时须要根据key的hash值与长度进行取模运算获得索引下标,若是是2的幂,那么length必定是偶数,则length-1必定是奇数,那么它对应的二进制的最后一位必定是1,因此它能保证h&(length-1)既能到奇数也能获得偶数,这样保证了散列的均匀性。相反若是不是2的幂,那么length-1多是偶数,这样h&(length-1)获得的都是偶数,就会浪费一半的空间了。
  • 运算效率高效。位运算比%运算高效。
二、 重复key的值会被新值替换,容许key为空且统一放在下标为0的链表上。
三、size大于等于阈值(容量*加载因子)时,会进行扩容。扩容机制是:扩容量为原来数组长度的两倍,根据扩容量建立新数组而后进行数组拷贝,新元素落位须要从新计算索引下标。扩容后,阈值须要从新计算,须要插入的元素落位的索引下标也须要从新计算。
四、扩容很耗时,而扩容的次数主要取决于加载因子的值,由于它决定这扩容的次数。下面讲下它的取值的重要性:
  • 加载因子越小,优势:存储的冲突机会减小;缺点:扩容次数越多(消耗性能就越大)、同时浪费空间较大(不少空间还没用,就开始扩容了)
  • 加载因子越大,有点:扩容次数较少,空间利用率高;缺点:冲突概率就变大了、链表(后面介绍)长度会变长,查找的效率下降。
五、扩容时会从新计算索引下标。也就是所谓的rehash过程
六、插入元素都是表头插入,而不是链表尾插入。
 

4.三、get()函数

知道了put方法的原理,那么get方法就很简单了。
public V get(Object key) {
    if (key == null)
        return getForNullKey();
    Entry<K,V> entry = getEntry(key);

    return null == entry ? null : entry.getValue();
}

第一步:若是key为空,则直接从table[0]所对应的链表中查找(应该还记得put的时候为null的key放在哪)。java

private V getForNullKey() {
        if (size == 0) {
            return null;
        }
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null)
                return e.value;
        }
        return null;
    }
第二步:若是key不为空,则根据key获取hash值,而后再根据hash和length-1取模获得索引,而后再遍历索引对应的链表,存在与key相等的则返回。
    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : 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 != null && key.equals(k))))
                return e;
        }
        return null;
    }

3、并发场景中使用HashMap会怎么样?

一、确定不能保证数据的安全性,由于内部方法没有一个是线程安全的。

二、有时会出现死锁状况。为何呢?下面列个场景简单分析下:

  • 假设当前容量为4, 有三个元素(a, b, c)都在table[2]下的链表中,另外一个元素(d)在table[3]下。如图

  • 假设此时有A,B两个线程都要往map中put一个元素则都须要扩容,当遍历到table[2]时,假设线程B先进入循环体的第一步:e 指向a, next指向b, 如图:
Entry<K,V> next = e.next;

  • 此时线程B让出时间片,让A线程一直执行完扩容操做,最终落位一样也是落位到table[2],其链表元素已经倒序了。如图:
  • A线程让出时间片,B线程操做:接着循环继续执行,执行到循环末尾的时候,table[2] 指向a, 同时 e 和 next 都是指向b,如图:
// 同理落位到2
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
// 指向a
newTable[i] = e;
e = next;

  • 接着第二轮循环, e = b, next = a, 进行第二轮循环后的结果是e = next 且 table[2] 指向b元素,b元素再指向a元素,如图:

  • 接着第三轮循环, e = a, a的下个元素为null, 因此next = null,可是当执行到下面这步就改变形式了,e.next 又指向了b,此时a和b已经出现了环形。由于next = null,因此终止了循环。
e.next = newTable[i];

 
  • 此时,问题尚未直接产生。当调用get()函数查找一个不存在的Key,而这个Key的Hash结果刚好等于3的时候,因为位置3带有环形链表,因此程序将会进入死循环!(上面图形均忽略四个元素和要插入元素的规划)

4、怎样合理使用HashMap?

  • 一、建立HashMap时,指定足够大的容量,减小扩容次数。最好为:须要存的实际个数/除以加载因子。可使用guava包中的Maps.newHashMapWithExpectedSize()方法。
为何要这样指定大小呢? 再去上面回顾下扩容时机吧
  • 二、不要在并发场景中使用HashMap,如硬要使用经过Collections工具类建立线程安全的map,如:Collections.synchronizedMap(new HashMap<String, Object>());
相关文章
相关标签/搜索