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);//ok hashMap.put("python",20);//ok hashMap.put("java",30);//替换 System.out.println("map="+hashMap); //解读HashMap源码 } }查看HashMap树化
- 当table中的一个结点的链表个数大于8个,时触发扩容table
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");//十二个 k-v } System.out.println(hashMap); } } class A{ private int num; public A(int num){ this.num = num; } //重写 hashCode方法,使A对象返回的hash值一样 @Override public int hashCode() { return 100; } @Override public String toString() { return "nA{" + "num=" + num + '}'; } }table列表触发扩容
-
当table列表中的元素到达12,时就会触发table的扩容
-
每次扩容翻倍
-
加载因子为容量的0.75,初始为12
- 演示代码
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 + '}'; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)