HashMap put 方法源码解析(纯属个人瞎写,方便自我记忆,如有问题,欢迎指出)
参考: HashMap源码解析JDK1.8. 十分详细,是个大佬
结论
1. hashmap 存储元素是 无序且唯一的.判断元素唯一的依据是元素的hash值以及equal方法.
– 故,使用hashmap存储自定义对象需要重写hashcode方法与equals 方法
– 添加属性相同的对象可以把 hashcode 设定成为一致
– Key值使用String类型有好处 ,String 不可变 hashcode值不会变 , 会被缓存 ,提升效率
– hashmap内部维护了一张Node<K,V> [ ] 类型的 tabel 表 (底层链表加数组)
用存放元素 例如: 添加元素前,对其进行内部 hash 方法处理 ,
假如算过之后的值为1-16, 重复的 就以链表的形式继续添加 ,不同的就以数组不同下标存放,当 存放数量大于数组长度的0.75时,就会扩容
如下图
0 | 1 | 2 | 3 | 4 | 5 | 6 | … |
---|---|---|---|---|---|---|---|
hash值相同,实际不同 | 元素某 | ASD | |||||
-hash值同,四季不同- | – | – | – | – | – | – |
准备工作
// 1. HashMap put方法起始 优先看putVal方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
----------------------------------------------
主方法内部使用方法说明
// HashMap 内部 hash 方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
// 内部维护 的 hash表
transient Node<K,V>[] table;
主方法内部使用方法说明
final Node<K,V>[] resize() {
//初始为 当前使用的存放数据的集合表
Node<K,V>[] oldTab = table;
// 未添加元素时的存放元素个数()
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//可以存放数量的临界值(当元素添加进3/4时就会扩容,不会等添加满以后扩容)
int oldThr = threshold;
//新容器 长度,临界值初始化
int newCap, newThr = 0;
//不为(第一次添加)初始的情况
if (oldCap > 0) {
// 最大 边界判断
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// 集合长度翻一倍 ,但是长度要小于最大值,大于初始值
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//新容器扩容(翻倍)
newThr = oldThr << 1; // double threshold
}
//初始容量设置为阈值
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
//第一次添加,初始长度 16
newCap = DEFAULT_INITIAL_CAPACITY;
//容器可存放内容阈值 12
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//这里是 自定义 容器初始大小时,临界值的设定
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
//链表数据结构插值操作..
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
添加元素断点
起始点
-------------------------------------------------
------ 起始点 --------
//参数描述 hash = 传入对象经过map
// 内部hash方法计算过后的值(由于hash存储是无序且唯一的,
//这个是判断当前put 对象是否能添加进集合的 条件之一)
// K =传入 键
// V =传入 值
// 后面两个不看
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
//tab 哈希数组,p 该哈希桶的首节点,n hashMap的长度,i 计算出的数组下标
Node<K,V>[] tab; Node<K,V> p; int n, i;
//第一次添加 与 扩容操作
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//判断后续添加元素位置 插入位置无值情况
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//插入位置有值情况
else {
Node<K,V> e; K k;
// hash 与 equal 判断唯一
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//略..
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
//计数器
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
----------------------
注意: debug 调试 时,进入putVal 方法 可能出现其他数据,可以在put 方法上打断点 使用Force Step Into 进入方法
关于HashMap的一些基本属性
默认初始大小
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
最大值
static final int MAXIMUM_CAPACITY = 1 << 30;
扩容因子 当数组长度达到 容器长度3/4时在次添加 符合部分条件的元素时就会扩容
static final float DEFAULT_LOAD_FACTOR = 0.75f;
属性伐值 当一个链表上有8个值得时候就会转换成红黑树
static final int TREEIFY_THRESHOLD = 8;
static final int UNTREEIFY_THRESHOLD = 6;
容器长度超过64也会转换成红黑树
static final int MIN_TREEIFY_CAPACITY = 64;