本章主要讲解HashMap初始化,根据源码,我们可以知道HashMap初始化主要有4个构造方法,接下来就具体来了解下它的4个构造方法:
一、无参构造方法
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
这个构造方法很简单,主要是将HashMap对象的负载因子初始化为默认的负载因子,其它的暂时什么也没有做;
二、带初始容量的构造方法
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
这个构造方法里面,调用了另一个构造方法,也就是带初始容量跟负载因子的构造方法,这里负载因子是设置为默认的负载因子,也就是跟上一个无参构造方法一样为0.75f;调用的另一个构造方法具体做了什么,我们接着看;
三、带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);
}
这个构造方法主要做了以下几个动作:
- 首先判断初始容量是否小于0,如果小于0则抛出IllegalArgumentException异常;
- 判断是否初始容量大于最大容量,如果是则初始容量设置为最大容量,及2的30次方
- 判断负载因子参数是否小于等于0,或者是否一个非正常浮点数,true则抛出IllegalArgumentException异常;
- 加载因子设置为参数的值;
- 这里发现调用了tableSizeFor方法,这个方法主要是根据传入的初始容量,对阀值进行一个处理,使它的值是2的n次方,什么意思呢,就比如你传入的是15,那么返回的值就是16,传入9,也是返回16;源码如下:
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;
}
四、参数为Map的构造方法,源码如下:
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
*/
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
可以看到,首先构造方法第一行,就是设置负载因子为默认的负载因子值;而第二行代码,则调用了putMapEntries,对参数进行了处理;具体进行了什么处理,一步步来看putMapEntries,首先看下方法源码内容:
/**
* Implements Map.putAll and Map constructor
*
* @param m the map
* @param evict false when initially constructing this map, else
* true (relayed to method afterNodeInsertion).
*/
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
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<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
这个方法有2个参数,一个是Map<? extends K, ? extends V> m,还有一个是boolean类型的evict,思考:evict这个值是什么意思,它的作用是什么?
回到putMapEntries这个方法,它主要的处理逻辑是:
- 首先获取入参Map的长度;
- 如果不大于0不做任何处理,否则判断当前table是否为空,注意table实际上是Node数组;如果为空,则进行运算判断是否超过阀值,如果超过,则重新设置阀值;
- 如果长度大于阀值,则进行扩容,调用resize()方法,这里跟负载因子进行运算,实际上涉及到hash冲突问题,也是为了减少冲突;
- 然后就是遍历map的entrySet,调用putVal,这个方法放在后面进行讲解;