HashMap 的一些认识: (JDK 1.7)
- 基于哈希表的Map接口的非同步实现,定义了键映射到值的规则
- 此实现提供所有可选的映射操作,并允许使用null值和null键
- 此实现假定哈希函数将元素适当分布在各桶之间,为读取操作提供稳定性能
- 迭代时间与实例容量(桶的数量)及其大小(键-值映射关系数)成正比
■ 类定义
public class HashMap<K,V>
extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
- 继承 AbstractMap抽象类,实现了Map接口
- 实现 Cloneable接口
- 实现 java.io.Serializable 接口,支持序列化
HashMap - “链表散列” (数组+链表), 数组每一项都是一条链的数据结构
//The default initial capacity - MUST be a power of two.
static final int DEFAULT_INITIAL_CAPACITY = 16;
//The maximum capacity - MUST be a power of two <= 1<<30.
static final int MAXIMUM_CAPACITY = 1 << 30;
//The load factor used when none specified in constructor.
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//The table, resized as necessary. Length MUST Always be a power of two.
transient Entry<K,V>[] table;
//The number of key-value mappings contained in this map.
transient int size;
//The next size value at which to resize (capacity * load factor).
int threshold;
//The load factor for the hash table.
final float loadFactor;
/**
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
transient int modCount;
■ 构造器
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;
//阈值为容量*负载因子和最大容量+1之间的最小值 以此值作为容量翻倍的依据(不能超过最大容量)
threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
//初始化一个2次幂的Entry类型数组 一个桶对应一个Entry对象
table = new Entry[capacity];
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
init();
}
- Entry
/**
* 静态类 默认实现内部Entry接口 (接口中可定义内部接口-Map.Entry接口为Map的内部接口)
* PS:JDK8中引入default,作用为在接口中定义默认方法实现
*/
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;//key具有引用不可变特性
V value;
Entry<K,V> next;//next指向下一个:单向链表,头插入
final int hash;
……
}
■ 主要方法
put(k, v)
/**
* @return key不存在返回null,否则返回旧值
*/
public V put(K key, V value) {
//其允许存放null的key和null的value
//当其key为null时,调用putForNullKey方法,放入到table[0]的这个位置(null键只有一个)
if (key == null)
return putForNullKey(value);
//通过调用hash方法对key进行哈希,得到哈希之后的数值
//其目的是为了尽可能的让键值对可以分不到不同的桶中
int hash = hash(key);
//根据上一步骤中求出的hash得到在数组中是索引i
int i = indexFor(hash, table.length);
//如果i处的Entry不为null,则通过其next指针不断遍历e元素的下一个元素。
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;//使用临时变量k主要用于e.key的赋值,意义有限
//hash一致 && (key引用相同 或 key字符串比较相同)
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
//值变更
V oldValue = e.