HashMap扩容与树化
HashMap源码
package com.lyc._Map;
import java.util.HashMap;
public class HashMapSource1 {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put("java",10);
hashMap.put("python",20);
hashMap.put("java",30);
System.out.println("map="+hashMap);
}
}
查看HashMap树化
- 当table中的一个结点的链表个数大于8个,时触发扩容table
-
满足链表大于8,table大于64时,触发的树化 演示用的代码
package com.lyc._Map;
import java.util.HashMap;
import java.util.Objects;
public class HashMapSource2 {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
for (int i=0;i<=12;i++){
hashMap.put(new A(i),"hello");
}
System.out.println(hashMap);
}
}
class A{
private int num;
public A(int num){
this.num = num;
}
@Override
public int hashCode() {
return 100;
}
@Override
public String toString() {
return "\nA{" +
"num=" + num +
'}';
}
}
table列表触发扩容
package com.lyc._Map;
import java.util.HashMap;
public class HashMapSource03 {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
for (int i = 0; i <100; i++) {
hashMap.put(new A2(i),"hello");
}
System.out.println(hashMap);
}
}
class A2{
private int num;
public A2(int num) {
this.num = num;
}
@Override
public String toString() {
return "\nA2{" +
"num=" + num +
'}';
}
}
|