Is there java.util.concurrent equivalent for WeakHashMap?
Solution 1:
Guava's CacheBuilder class allows you to do this easily.
CacheBuilder.newBuilder().weakKeys().build()
Note that this changes key equality semantics to be ==
instead of .equals()
which will not matter in your case of using Class
instances but is a potential pitfall.
Solution 2:
I don't believe there is. In fact the javadoc suggests using Collections.synchronizedMap()
"Like most collection classes, this class is not synchronized. A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap method."