WeakhashMap 是如何工作的?

发布于 2024-10-02 23:25:22 字数 54 浏览 0 评论 0原文

正如weakhashmap如何理解对其键之一的引用现在已经过时,特别是如果该键是池化的字符串?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-10-09 23:25:22

您不能将字符串文字与 WeakHashMap 一起使用(当然可以,但这样做没有意义):

String myKey = "somekey";

相反,您必须使用:

String myKey = new String("somekey");

在后一种情况下,字符串不会被池化。

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.

站稳脚跟 2024-10-09 23:25:22

“过时”这个词并不精确。所讨论的情况是“垃圾收集”。当键被垃圾收集时,值将从 WeakHashMap 中删除。时期。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文