Hashing :How Hash Map Works In Java Or How Get() Method Works Internally
How hashmap works in java
http://kakajw.iteye.com/blog/935226
http://www.importnew.com/7099.html
http://xiaolu123456.iteye.com/blog/1485349
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.
DEFAULT_INITIAL_CAPACITY常量:
The default initial capacity - MUST be a power of two (2 ^ n: 2的n次方).
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
设定 initialCapacity
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
DEFAULT_LOAD_FACTOR常量:
The load factor used when none specified in constructor.
static final float DEFAULT_LOAD_FACTOR = 0.75f;
设定 loadFactor
public HashMap(int initialCapacity, float loadFactor) { ... }
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
resize方法:
resize() { ... }
Initializes or doubles table size. If null, allocates in accord with initial capacity target held in field threshold. Otherwise, because we are using power-of-two expansion, the elements from each bin must either stay at same index, or move with a power of two offset in the new table.