//默认的初始化容量为16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//最大的容量,容量的值必须是2的幂并且小于最大的容量,最大值为2的30次方
static final int MAXIMUM_CAPACITY = 1 << 30;
//加载因子默认值为0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//计数阈值,超过这个值将会使用树形结构替代链表结构
static final int TREEIFY_THRESHOLD = 8;
//由树形结构转换成链表的阈值
static final int UNTREEIFY_THRESHOLD = 6;
//树形结构最小的容量为64
static final int MIN_TREEIFY_CAPACITY = 64;
//链表数组
transient Node<K,V>[] table;
//HashMap中value的集合
transient Set<Map.Entry<K,V>> entrySet;
//HashMap的长度
transient int size;
//调整大小的下一个大小值,阈值
int threshold;
//hashtable的加载因子
final float loadFactor;
- get方法
public V get(Object key) {
Node<K,V> e;
//对key进行hash操作 高位参与运算 让key均匀分布 减少hash冲突
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//非空判断 如果为空 直接返回null value 为null
if ((tab = table) != null && (n = tab.length) > 0 &&
//判断 key的hash 在table数组中的位置
(first = tab[(n - 1) & hash]) != null) {
// 判断第一个元素是否是要查询的元素 出现在第一个位置的概率相对大些 相对分散稀疏的hashmap大部分会存在第一个元素
if (first.hash == hash && // always check first node
//如果 hash相同并且key相同则返回 如果hash冲突了,要比较key的值
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//下一个结点是否为空
if ((e = first.next) != null) {
//如果第一个结点是树结构,则使用getTreeNode查询相应的数据
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {//非树结构 循环节点判断
//hash 相等并且key 相同 则返回此结点
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
- put方法
public V put(K key, V value) {
//对key进行hash操作 高位参与运算 让key均匀分布 减少hash冲突
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//在第一次put的时候判断table是否
if ((tab = table) == null || (n = tab.length) == 0)
//为空进行初始化数组在resize()中创建数组
n = (tab = resize()).length;
// 根据 key 的哈希值计算出要插入的数组索引 i
//如果对应数据i下标为null 则 直接插入元素
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//如果对应数据i下标不为null 判断hash是否冲突 是否同一个key 如果 key 已经存在了,直接覆盖 value
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;
}
}
// 如果存在相同的key 覆盖旧值
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;
}
-resize方法
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
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
newCap = DEFAULT_INITIAL_CAPACITY;
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;
}