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 ?
WeakHashMap
doesn't make that determination; rather, the normal Java garbage collection process deletes unreferenced keys. Thep
key will be gone as soon as Java garbage collection is triggered.What
WeakHashMap
does is it uses weak references to refer to keys, so the garbage collector knows not to count theWeakHashMap
references to the map keys as "holding" the key objects in memory.