我已经将一个对象作为键存储在WeakHashMap
. 现在,如果我更改它的值,然后调用 GC 和 print map
,那么什么都没有。
public static void main(String[] args) throws InterruptedException{
WeakHashMap map = new WeakHashMap();
Integer obj = new Integer(200);
map.put(obj, "sgdjsgd");
obj=new Integer(20);
System.gc();
Thread.sleep(2000);
System.out.println(map);
}
- 预期输出:
{200,"sgdjsgd"}
- 实际输出:
{}