I need a simple hash map with weak keys. Java's own WeakHashMap
gives me that, but not with identity semantics (it uses equals()
for key comparison).
我需要一个带弱键的简单哈希映射。 Java自己的WeakHashMap给了我这个,但不是身份语义(它使用equals()进行密钥比较)。
Google's Guava library has revamped its hash map methods. In the latest version (14.0), instead of using a MapMaker
(which has many if not most things deprecated), I apparently should now use a CacheBuilder
, which all sorts of options. Fine, it has a weakKeys()
option, so that's what I'll use. But the resulting cache is also concurrent (i.e. it keeps various maps inside and uses its own internal set of keys to regulate access concurrently), and I can't turn that off; I don't need concurrency, as I'm already using my own ReadWriteLock
to govern access to my map.
Google的Guava库已经改进了它的哈希映射方法。在最新版本(14.0)中,我现在显然应该使用CacheBuilder,而不是使用MapMaker(它有很多,如果不是大多数东西都弃用了),它有各种各样的选项。好吧,它有一个weakKeys()选项,所以我将使用它。但是生成的缓存也是并发的(即它将各种映射保留在内部并使用它自己的内部密钥集来同时调节访问),我无法将其关闭;我不需要并发,因为我已经使用自己的ReadWriteLock来管理对我的地图的访问。
Fine, I'll accept concurrency; just give me the map! I try:
好吧,我会接受并发;给我一张地图!我试试:
Map<Foo, Bar> map = CacheBuilder.newBuilder().weakKeys().build();
Wait, that gives me back a Cache<Object, Object>
, which isn't a Map<Foo, Bar>
! How can I get a simple identity-based weakly-keyed map in Google Guava?
等等,这给了我一个Cache ,object>
2 个解决方案
#1
2
Assuming you're willing to live with concurrency, you're almost there:
假设你愿意和并存,你几乎就在那里:
CacheBuilder.newBuilder().weakKeys().build().asMap();
But that said...your use case isn't entirely clear, that is, why you need a map with these properties, and why you need a map with identity semantics when the keys have another notion of equality.
但是那说......你的用例并不完全清楚,也就是说,为什么你需要一个具有这些属性的地图,以及为什么当密钥具有另一个相等概念时你需要一个带有身份语义的地图。
#2
2
I need a simple hash map with weak keys.
我需要一个带弱键的简单哈希映射。
I don't follow why you want to switch from MapMaker
to CacheBuilder
then. It sounds like what you want is what MapMaker
is there for.
我不遵循为什么要从MapMaker切换到CacheBuilder。听起来你想要的就是MapMaker的用途。
You may not think you need concurrency, but remember that GC can run concurrently, and cause stale entries to be cleaned up. Anyway, the concurrency won't even cost you much.
您可能认为不需要并发,但请记住GC可以并发运行,并导致过时的条目被清除。无论如何,并发性甚至不会让你付出太多代价。
#1
2
Assuming you're willing to live with concurrency, you're almost there:
假设你愿意和并存,你几乎就在那里:
CacheBuilder.newBuilder().weakKeys().build().asMap();
But that said...your use case isn't entirely clear, that is, why you need a map with these properties, and why you need a map with identity semantics when the keys have another notion of equality.
但是那说......你的用例并不完全清楚,也就是说,为什么你需要一个具有这些属性的地图,以及为什么当密钥具有另一个相等概念时你需要一个带有身份语义的地图。
#2
2
I need a simple hash map with weak keys.
我需要一个带弱键的简单哈希映射。
I don't follow why you want to switch from MapMaker
to CacheBuilder
then. It sounds like what you want is what MapMaker
is there for.
我不遵循为什么要从MapMaker切换到CacheBuilder。听起来你想要的就是MapMaker的用途。
You may not think you need concurrency, but remember that GC can run concurrently, and cause stale entries to be cleaned up. Anyway, the concurrency won't even cost you much.
您可能认为不需要并发,但请记住GC可以并发运行,并导致过时的条目被清除。无论如何,并发性甚至不会让你付出太多代价。