Hash Map源码分析
1.数据结构
1.1 JDK 1.7
数组 + 链表
transient Entry<K,V>[] table;
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
}
存储结构是以数组的形式存储了Entry对象,而每个Entry对象内包含key、value、hash四个字段。
其中next字段的作用是是保存下一个Entry的(这里的Entry其实就是一个单链表结构,相同Hash值会被放入链表中)
1.2 JDK 1.8
数组 + 链表 or 红黑树
//Entry更名为 Node
transient Node<K,V>[] table;
//链表长度超过8且数组长度大于64,则将链表转换成红黑树
static final int TREEIFY_THRESHOLD = 8;
//在1.8中节点名字改成了Node
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
//红黑树
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // 父节点
TreeNode<K,V> left; // 左节点
TreeNode<K,V> right; // 右节点
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red; // 是否红节点
TreeNode(int hash, K key, V val, Node<K,V> next) {
super(hash, key, val, next);
}
}
//继承该类是为给LinkerHasMap做准备
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
除了将Entry命名更改为Node外,存储结构的变化是,当数组中的链表长度大于8并且数组长度大于64时,自动将链表变成红黑树结构,增加查找效率。
2.基本概念
2.1 负载因子和阈值
/**
* 默认容量(需要是2的幂次⽅,原因是HashMap会运用大量的位运算,使用2的幂次⽅的值会方便运算)
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* 负载因子
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
在介绍负载因子和阈值之前首先介绍一下公式:
阈值 = 当前数组长度 * 负载因子
负载因子是一个百分比,作用就是控制什么时候来扩容,而阈值就是扩容判断中一个具体的临界点值。
3.源码分析
3.1 JDK 1.7
3.1.1 初始化与属性
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
//默认初始容量,必须是2的幂 这里的值是16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//最大容量
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的负载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//默认的空数组
static final Entry<?,?>[] EMPTY_TABLE = {
};
//用来盛放真实数据的数组
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
//当前HashMap的真实键值对数量
transient int size;
//阈值 = 数组长度*负载因子(在初始化时直接取DEFAULT_INITIAL_CAPACITY的值 也就是16)
int threshold;
//负载因子
final float loadFactor;
//标识对该HashMap进行结构修改的次数,结构修改是指增删改或其他修改其内部结构(例如rehash)的次数.
//用于迭代器快速失败.
transient int modCount;
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
//可以同时制定数组大小和负载因子
public HashMap(int initialCapacity, float loadFactor) {
...//省略部分逻辑判断
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
...
this.loadFactor = loadFactor;
threshold = initialCapacity;
...
}
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
}
}
3.1.2 保存(put)
public V put(K key, V value) {
//1. 数组为空 -> 初始化(创建)数组
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//2. key为null,单独处理
if (key == null)
return putForNullKey(value);
//3. 计算hash值
int hash = hash(key);
//4. 计算该hash值该存放在数组的哪个索引处
int i = indexFor(hash, table.length);
//5. 遍历链表(数组的每个元素都是单链表的表头) 查找链表中是否已存在相同的key 如果有,则替换掉并且return
for (Entry<K,V> e = table[i]; e != null; e = e.next) {