Is there a non-jdk implementation for a parallel weak hash map?

I need a parallel weak hash mapping where keys are compared to equality rather than id like in WeakHashMap.

The answers to this similar old question:

Is there a java.util.concurrent equivalent for WeakHashMap?

made it clear that there is nothing like this in the JDK. However, his accepted answer mistakenly suggests that Guava CacheBuilder can do this, but CacheBuilder uses identity comparison rather than equality comparison as needed.

A previous similar question was asked a few years ago while something has changed and maybe a third party library currently provides the implementation I need?

I could implement this using WeakHashMap by storing the object in the cache as a map key and loosely referencing it as its value, but it would not be thread safe.

Wrapping a WeakHashMap in Collections.synchronizedMap is not really an option as the assertion would be too high.

If there is currently no alternative, I'm considering injecting something wrapper-style ConcurrentHashMap on top of WeakHashMap, but is that really a good idea? Has anyone done this before?

Use case:

The reason I am asking this is because I am implementing cache streams with single values ​​(not a key value association like in the Google Cache class) and I want these values ​​to be garbage collected when references on them do not exist.

+3


source to share





All Articles