HashMap 储值原理
一.当创建HashMap对象时 成员变量的变化
this.loadFactor = DEFAULT_LOAD_FACTOR; ( 加载因子)public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted }
//底层哈西表内顺序表默认长度
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16//底层哈西表 最大长度
static final int MAXIMUM_CAPACITY = 1 << 30; (左移30位)//加载/负载 因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;//树化阈值 (由链表转成树)
static final int TREEIFY_THRESHOLD = 8;//反树化阈值 (由树转成链表)
static final int UNTREEIFY_THRESHOLD = 6;//树化 底层哈西表内顺序表最小长度
static final int MIN_TREEIFY_CAPACITY = 64;//底层哈西表内顺序表 数据类型
transient Node<K,V>[] table;//nullstatic class Node<K,V> implements Map.Entry<K,V> { 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; }
//键值对的数量
transient int size;//0如果顺序表内有价值数量 > 阈值 进行扩容 16*0.75f = 12
int threshold;//成员变量 加载/负载因子
final float loadFactor;二.在进行数据添加时 HashMap底层的处理情况
2.0第一次添加
底层Node[] 进行了空间开辟 长度为16 阈值 12(会把key=null的键值对放到顺序表0的位置) (源码中的table相当于哈希表)
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { 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; 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; }
2.1添加的数据 key 为null
将数据存到 顺序表下标为0的位置
2.2添加的数据 key 不为null
(哈希值的计算 目的是减少哈希位置的冲突) 重写hashCode()方法
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
2.2.1 添加数据到指定位置没有值
直接添加 (因为没有哈希值的冲突)if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null);
2.2.2 添加数据指定位置有值 hash一样 key 不一样
在现有节点的后面进行追加 七上 八下 jdk7 jdk8for (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; }
2.2.3 添加数据指定位置有值 hash一样 key 一样
if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; }
1.将新的value替换旧的value
2.将旧的value返回
2.2.4 扩容
if (++size > threshold) resize(); afterNodeInsertion(evict);
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 }
普通扩容
容量 << 1
阈值 <<1threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
树化 (要求: 1.某一个位置 节点数量>=8 且 底层数组长度>=64)
1.某一个位置 节点数量>=8 (触发树化 进入到树化的方法中还需要判断此时数组的长度是否>=64,如果成立,在进行最后的安全校验添加数据的位置不为null ) 此时才开能树化。if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash);
final void treeifyBin(Node<K,V>[] tab, int hash) { int n, index; Node<K,V> e; if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize(); else if ((e = tab[index = (n - 1) & hash]) != null) { TreeNode<K,V> hd = null, tl = null; do { TreeNode<K,V> p = replacementTreeNode(e, null); if (tl == null) hd = p; else { p.prev = tl; tl.next = p; } tl = p; } while ((e = e.next) != null); if ((tab[index] = hd) != null) hd.treeify(tab); } }
2.底层数组长度>=64
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize();
3.添加数据的位置不为null (安全期间校验)
DeBug调试代码
public class MyData {
String name;
int age;
public MyData(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyData myData = (MyData) o;
return age == myData.age &&
Objects.equals(name, myData.name);
}
@Override
public int hashCode() {
if(age%2==0){
return 1;
}
return age;
}
@Override
public String toString() {
return "MyData{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class MapTest {
@Test
public void test03() {
HashMap<MyData, Integer> map = new HashMap<>();
//key = null 将数据添加到下标为0的位置
//第一次添加的时候才会给底层数组开辟空间
map.put(null, 20);
for (int i = 0; i < 1000; i++) {
map.put(new MyData("MyData" + i, i), 10);
}
}
}