问题
As in how does weakhashmap understand that a reference to one of its key is obsolete now especially if the key is a String which is pooled?
回答1:
You must not use String literals with WeakHashMap (well you can but there would be no point in it):
String myKey = "somekey";
instead you must use:
String myKey = new String("somekey");
In the latter case String is not pooled.
回答2:
The word 'obsolete' is imprecise. The condition in question is 'garbage-collected'. A value is removed from the WeakHashMap when and if the key is garbage-collected. Period.
来源:https://stackoverflow.com/questions/4196448/how-does-weakhashmap-work