I need a concurrent weak hash map where keys are compared with equality and not identity, as in WeakHashMap.
The answers to this similar old question:
Is there java.util.concurrent equivalent for WeakHashMap?
made it clear that there is not something like that in the JDK. However, its accepted answer wrongly suggests that Guava's CacheBuilder can do that, but CacheBuilder makes use of identity comparison, not equality comparison as I require.
The previous similar question was asked several years ago, in the meanwhile something has changed and maybe nowadays a third party library provides the implementation I need?
I could implement this using a WeakHashMap by means of storing the object to cache as the key of the map and a weak reference to it as its value, but it would not be thread safe.
Wrapping a WeakHashMap in a Collections.synchronizedMap is not really an option since the contention will be too high.
If no alternative currently exists, I am considering implementing something in the style of a ConcurrentHashMap wrapper over WeakHashMap, but is that really a good idea? Has somebody done that before?
Use case:
The reason I am asking this is because I am implementing a thread safe cache of single values (not a key-value association as in Google's Cache class) and I want these values to be garbage collected when no reference to them exists.