开发者

Soft/Weak key MapMAker with equals for key

开发者 https://www.devze.com 2022-12-30 08:54 出处:网络
I need a Concurrent Hash Map with Weak or Soft keys were the equality is equals and not ==. For this kind of keys, google collection chooses == by default.

I need a Concurrent Hash Map with Weak or Soft keys were the equality is equals and not ==.

For this kind of keys, google collection chooses == by default.

Is there a w开发者_C百科ay to override this choice? How should I proceed?

Best regards,

Nicolas.


You can't do that in google-collections. You can't do it in guava either, currently. However, they have added an Equivalence interface and the implementations you'd expect for it (equals, null-aware equals and ==) recently and it seems like they might allow you to specify what Equivalence should be used for keys/values in the future (see this issue). MapMaker code seems to be undergoing some changes at this time.


You can use java.util.WeakHashMap, wrapped with a call to Collections.synchronizedMap()

It won't be as fast as a ConcurrentHashMap if thread contention is significant. But it has the behaviour you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消