how does weakhashmap work? [duplicate]

余生颓废 提交于 2019-12-09 17:02:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!