简体   繁体   English

IdentityHashMap和WeakHashMap的组合

[英]Combo of IdentityHashMap and WeakHashMap

I need a Map implementation that shares properties of both IdentityHashMap and WeakHashMap (reference equality instead of equals() and weak references on keys). 我需要一个Map实现,它共享IdentityHashMapWeakHashMap属性(引用相等而不是equals()和键上的弱引用)。

What implementation do you recommend (it has to work on Android)? 你推荐什么实现(它必须在Android上工作)?

If you're suggesting Guava, then new MapMaker().weakKeys().makeMap() would do the job directly, since weakKeys uses reference equality for keys. 如果你建议使用Guava,那么new MapMaker().weakKeys().makeMap()将直接完成这项工作,因为weakKeys使用引用相等的键。

The documentation of weakKeys says: weakKeys的文档说:

Specifies that each key (not value) stored in the map should be wrapped in a WeakReference (by default, strong references are used). 指定存储在映射中的每个键(不是值)应该包装在WeakReference中(默认情况下,使用强引用)。 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. 警告:使用此方法时,生成的映射将使用标识(==)比较来确定键的相等性,这是对Map规范的技术违反,可能不是您所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM