# HashMap源码解析——构造方法

HashMap的几个构造方法：

//加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//容量
static final int MAXIMUM_CAPACITY = 1 << 30;
static final int MIN_TREEIFY_CAPACITY = 64;

public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}

public HashMap(int initialCapacity) {
}

public HashMap(int initialCapacity, float loadFactor) {
//确保数字合法
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
throw new IllegalArgumentException("Illegal load factor: " +
this.threshold = tableSizeFor(initialCapacity);
}

public HashMap(Map<? extends K, ? extends V> m) {
putMapEntries(m, false);
}

    /**
* Returns a power of two size for the given target capacity.
*/
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;
}

#### Java容器（四）：HashMap（Java 7）的实现原理

2017-02-09 13:48:08

#### java提高篇（二三）-----HashMap

2014-01-15 21:41:46

2017-02-13 10:25:03

#### HashMap原理及详解

2016-09-09 10:39:10

#### 深入理解HashMap（精华必看）

2017-01-13 12:00:07

#### HashMap图解--转载

2017-06-15 23:18:24

#### HashMap底层实现原理

2017-03-09 18:41:41

#### HashMap的实现方式

2018-01-23 22:53:16

#### HashMap类源码解析

2017-10-26 14:00:29

#### HashMap

2018-03-26 11:44:04