gpt4 book ai didi

java - IdentityHashMap 和 WeakHashMap 的组合

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:09 24 4
gpt4 key购买 nike

我需要一个 Map 实现,它共享 IdentityHashMapWeakHashMap 的属性(引用相等而不是 equals() 和键上的弱引用)。

您推荐哪种实现(它必须在 Android 上运行)?

最佳答案

如果您建议使用 Guava,那么 new MapMaker().weakKeys().makeMap() 会直接完成这项工作,因为 weakKeys 使用键的引用相等性.

weakKeys 的文档说:

Specifies that each key (not value) stored in the map should be wrapped in a WeakReference (by default, strong references are used). Warning: when this method is used, the resulting map will use identity (==) comparison to determine equality of keys, which is a technical violation of the Map specification, and may not be what you expect.

关于java - IdentityHashMap 和 WeakHashMap 的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910375/

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