包含创建,添加,删除,迭代,打印
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1,1);
map.put(2,2);
map.put(3,3);
map.remove(2);
System.out.println("map[3]=" + map.get(3));
System.out.println(map);
一贯原则,没用的帮兄弟们省掉了
package java.util;
// AbstractMap,可以随机访问
public class HashMap<K, V> extends AbstractMap<K, V> {
// 初始容量
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
// 最大容量
static final int MAXIMUM_CAPACITY = 1 << 30;
// 负载因子,当前容量超过最大容量*负载因子,开始扩容
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// 链表大于8树化
static final int TREEIFY_THRESHOLD = 8;
// 树小于6链化
static final int UNTREEIFY_THRESHOLD = 6;
// 树化的最小容量
static final int MIN_TREEIFY_CAPACITY = 64;
// 存储key,value
static class Node<K, V> implements Map.Entry<K, V> {
// 通过key中字段计算
final int hash;
final K key;
V value;
Node<K, V> next;
Node(int hash, K key, V value, Node<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
// 省略get,set等模板方法
}
}
// 负载因子
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public V put(K key, V value) {
// 通过hash()对key进行一次hash()
return putVal(hash(key), key, value);
}
// 重点分析
final V putVal(int hash, K key, V value) {
// tab,桶,就是数组
// p,期望对象
// n,当前桶容量
// i,桶下标
Node<K,V>[] tab; Node<K,V> p; int n, i;
// 如果是第一次,通过resize()更新初始容量
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 与桶大小&计算,二次hash()
// 刚好桶上没元素,直接在桶上更新
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
// e,辅助期望元素
// k,桶下标元素的key
Node<K,V> e; K k;
// 如果和桶第一个一样
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)
treeifyBin(tab, hash);
break;
}
// 找到key一样的
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
e.value = value;
}
// 扩容
if (++size > threshold)
resize();
return null;
}
// key经过hashCode()与低位^,让高位也参与计算
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
// 二倍扩容
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)
// 通过hash&新桶容量,计算位置
newTab[e.hash & (newCap - 1)] = e;
// 树转移
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
// 链表
else { // preserve order
// lo,low,hi,high,high=桶容量/2+low
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
// 如果在下标0位置
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;
}
// 调用的内部方法
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
// 如果是头节点
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
// 获取树节点
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
// 链表处理
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
// p在e之后,if breadk时,p.next=e
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
// 树处理
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
// 链表头节点处理
else if (node == p)
tab[index] = node.next;
// 链表非头结点
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
注意,map的删除中并没有resize()逻辑
// 封装的内部逻辑
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
// 如果是桶上
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
// 树处理
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 遍历链表
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
获取逻辑和删除差不多
在同下标元素>=8时开始树化,内部通过红黑树保持平衡,提高查找效率。发但生树化的概率很小(0.00000006),且里面的代码很多(主要是红黑树的平衡逻辑),就不重点展开了。
里面代码确实挺多,其中还包括树,以后分析并尝试重写。