How does it know this key is old and ready to throw it away ? and why string literal ?
For example,
private static WeakHashMap<<? extends Object>, String> m =
new WeakHashMap<<? extends Object>, String>();
public static void A(){
Point p = new Point();
m.put(p, "a");
}
does it mean that 'p' key will be gone as soon as A() returns ?
WeakHashMapdoesn't make that determination; rather, the normal Java garbage collection process deletes unreferenced keys. Thepkey will be gone as soon as Java garbage collection is triggered.What
WeakHashMapdoes is it uses weak references to refer to keys, so the garbage collector knows not to count theWeakHashMapreferences to the map keys as "holding" the key objects in memory.