Java
HashMap
⼀、概述
在官⽅⽂档中是这样描述HashMap的:
Hash table based implementation of the Map interface. This implementation provides
all of the optional map operations, and permits null values and the null key. (The
HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
and permits nulls.) This class makes no guarantees as to the order of the map; in
particular, it does not guarantee that the order will remain constant over time.
⼏个关键的信息:基于Map接⼝实现、允许null键/值、⾮同步、不保证有序(⽐如插⼊的顺序)、
也不保证序不随时间变化。
在HashMap中有两个很重要的参数,容量(Capacity)和负载因⼦(Load factor)
Initial capacity: The capacity is the number of buckets in the hash table, The initial
capacity is simply the capacity at the time the hash table is created.
Load factor: The load factor is a measure of how full the hash table is allowed to get
before its capacity is automatically increased.
简单的说,Capacity就是bucket的⼤⼩,Load factor就是bucket填满程度的最⼤⽐例。如果对迭
代性能要求很⾼的话不要把capacity设置过⼤,也不要把load factor设置过⼩。当bucket中的
entries的数⽬⼤于capacity*load factor时就需要调整bucket的⼤⼩为当前的2倍。
⼆、构造函数
HashMap底层维护⼀个数组,当新建⼀个HashMap的时候,就会初始化⼀个数组。我们看⼀下
JDK源码中的HashMap构造函数:
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);
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
init();
}
可以看到其中⼀⾏为 table = new Entry[capacity]; 。在构造函数中,其创建了⼀个Entry的数
组,其⼤⼩为capacity,那么Entry⼜是什么结构呢?看⼀下源码:
transient Entry<K,V>[] table;
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
……
}
HashMap中的是通过transient Entry[] table来存储数据,该变量是通过transient进⾏修饰的。
Entry是⼀个static class,其中包含了key和value,也就是键值对,另外还包含了⼀个next的
Entry指针。我们可以总结出:Entry就是数组中的元素,每个Entry其实就是⼀个key-value对,它
持有⼀个指向下⼀个元素的引⽤,这就构成了链表。
大家一起学习丫~