概述
本文主要分析HashMap类的源码
HashMap继承结构
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
HashMap继承结构图
可以看到:
1、实现了 Map 接口,拥有一组map的通用操作
2、实现了 Cloneable 接口,可进行拷贝,需要注意的是 hashmap 是浅拷贝,修改拷贝对象对源对象会产生影响
3、实现了 Serializable 接口,实现了序列化,可保存于本地
HashMap的数据结构
数组 + 单向链表
HashMap的成员变量
/**
* The default initial capacity - MUST be a power of two.
* 默认初始容量
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
* 最大容量
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The load factor used when none specified in constructor.
* 默认负载因子,用来指示元素个数占整体容量的比例,当元素达到这个比例的时候,就准备扩容了
* 为什么加载因子是0.75?【经常被问到的面试题】
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* The table, resized as necessary. Length MUST Always be a power of two.
* 用来存储键值对的Entry数组
*/
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).
* 阈值 = capacity(容量)* loadFactor(负载因子。这个值是当前已占用数组长度的最大值。size 超过这个值就重新 resize(扩容),
* 扩容后的 HashMap 容量是之前容量的两倍。默认的临界值是12 = 16 * 0.75
*/
int threshold;
/**
* The load factor for the hash table.
* 哈希表的负载因子,默认是0.75f
*/
final float loadFactor;
/**
* 每次扩容和更改 map 结构的计数器
*/
transient int modCount;
Entry必须要介绍一下,看源码
static class Entry<K,V> implements Map.Entry<K,V> {
final K key; // 存储的key
V value; // 存储的value
Entry<K,V> next; // next指针,指向下一个节点
int hash; // 哈希值
可以看到,Entry 是 HashMap 的静态内部类,key 是存储的键,value 是存储的值,由于HashMap的数据结构是数组 + 链表,所以 next 是链表指向下一个节点的指针,hash 是哈希值
HashMap的构造方法
两个参数的构造方法
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and load factor.
* 两个参数的构造方法
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
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
// 传入的容量并不一定是2的n次幂,找到最小的二次幂容量
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
// 实际负载因子
this.loadFactor = loadFactor;
// 阈值
threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
// 根据容量创建 table 数组
table = new Entry[capacity];
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
// 空方法,留给用户自己扩展
init();
}
一个参数的构造方法
// 一个参数的构造方法,使用默认负载因子0.75
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
无参构造函数
public HashMap() {
//传入默认大小,和加载因子
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
put方法
public V put(K key, V value) {
// 如果key为null,单独处理
if (key == null)
return putForNullKey(value);
int hash = hash(key);//计算出哈希值
int i = indexFor(hash, table.length);//通过哈希值计算出哈希桶的位置
//遍历链表,判断是否有key相等(即哈希值相等)的节点,如果有,则更新值,并返回旧的值
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++; // 更新次数+1
addEntry(hash, key, value, i); // 没有找到相同的
return null;
}
putForNullKey()方法
// 如果key为null
private V putForNullKey(V value) {
// key为null的键值对,默认存放在0号位置
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
// 覆盖相同的值
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0); // 没有找到相同的
return null;
}
hash()方法
final int hash(Object k) {
int h = 0;
if (useAltHashing) {
if (k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h = hashSeed;
}
h ^= k.hashCode();
// 通过多次位运算进行扰动,减少哈希冲突
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
indexFor()方法
// 计算Entry在数组中的位置
static int indexFor(int h, int length) {
// 和长度-1进行与运算,相当于进行模n计算
return h & (length-1);
}
addEntry()方法
void addEntry(int hash, K key, V value, int bucketIndex) {
//判断是否要扩容
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length); // 扩容
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
resize()方法
void resize(int newCapacity) {
Entry[] oldTable = table; // 旧的数组
int oldCapacity = oldTable.length; // 旧的容量
if (oldCapacity == MAXIMUM_CAPACITY) { // 如果本身达到了最大的容量
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity]; // 创建新的数组,容量为原来的2倍
boolean oldAltHashing = useAltHashing;
useAltHashing |= sun.misc.VM.isBooted() &&
(newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
boolean rehash = oldAltHashing ^ useAltHashing;
transfer(newTable, rehash); // 迁移旧的Entry到扩容后的数组中
table = newTable; // 将table指向新的数组
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); // 重新计算阈值
}
createEntry()方法
//头插法在这里
void createEntry(int hash, K key, V value, int bucketIndex) {
//先获取头结点
Entry<K,V> e = table[bucketIndex];
//创建新的节点指向头结点,使其成为新的头结点
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
transfer()方法
/**
* 将所有元素转移到新容器中 newTable。在转移的过程中会产生并发问题,产生 环状,在 get 元素的时候产生死循环
* newCapacity: 容量翻倍的新的空数组
* 双重循环遍历数组+链表
*/
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
HashMap1.7的put()方法整体流程如下图:
注意:
HashMap1.7是使用头插法插入节点的,在进行扩容调用resize方法,进而调用transfer方法迁移元素的时候,如果多线程并发,就有可能出现链表死循环的问题。
get()方法
public V get(Object key) {
if (key == null) // key为null单独处理
return getForNullKey();
Entry<K,V> entry = getEntry(key); // 获取元素
return null == entry ? null : entry.getValue();
}
// 如果key为null,就去数组中0位置去查找
private V getForNullKey() {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key); // 计算哈希值
//遍历Entry,判断是否存在相同的key
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
// 如果发现相同的key,就返回value
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
get方法先根据hash值计算出在数组中的位置,如果发现相同的key的Entry,就返回值。
remove()方法
public V remove(Object key) {
Entry<K,V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}
final Entry<K,V> removeEntryForKey(Object key) {
int hash = (key == null) ? 0 : hash(key);// 计算hash值
int i = indexFor(hash, table.length); // 计算数组中的位置
Entry<K,V> prev = table[i];
Entry<K,V> e = prev;
while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e) // 如果删除的是头节点
table[i] = next; // 那么下个节点就作为新的头节点
else // 删除的是中间节点
prev.next = next; // 前驱节点指向下一个节点
e.recordRemoval(this); // 空方法
return e;
}
prev = e; // 记录前驱节点
e = next;
}
return e;
}
remove方法通过hash值计算出在数组中桶的位置,然后遍历桶中的Entry,找到相同的key就删除。