package com.itjava.interview;
import java.util.HashMap;
import java.util.WeakHashMap;
public class WeakHashMapDemo {
public static void main(String[] args) {
myHashmap();
System.out.println("=====================");
myWeakHashMap();
}
private static void myHashmap(){
HashMap<Integer, String> map = new HashMap<>();
Integer key = new Integer(1);
String value = "HashMap";
map.put(key,value);
System.out.println(map);
key = null; //key已经被存到map中的 node节点中去了,只与其中的值有关,即1
System.out.println(map);
System.gc();
System.out.println(map+"\t"+map.size());
}
private static void myWeakHashMap() {
WeakHashMap<Integer,String> map = new WeakHashMap<>();
Integer key = new Integer(2);
String value = "WeakHashMap";
map.put(key,value);
System.out.println(map);
key = null;
System.out.println(map);
System.gc();
System.out.println(map+"\t"+map.size()); //gc之后被回收
}
}
WeakHashMap 弱引用hashMap
最新推荐文章于 2022-12-28 22:49:49 发布