gpt4 book ai didi

java - 为什么存在WeakHashMap,却没有WeakSet?

转载 作者:IT老高 更新时间:2023-10-28 11:53:30 25 4
gpt4 key购买 nike

来自 J. Bloch

A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a WeakHashMap.

那么,为什么 Java 中没有 WeakSet Collections framework ?

最佳答案

Collections.newSetFromMap

Set<Object> weakHashSet = 
Collections.newSetFromMap(
new WeakHashMap<Object, Boolean>()
);

Collections.newSetFromMap 中所示文档,通过 WeakHashMap获取 Set .

关于java - 为什么存在WeakHashMap,却没有WeakSet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com