Soft/Weak key MapMAker with equals for key
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.
精彩评论