Is there java.util.concurrent equivalent for WeakHashMap?

前端 未结 6 1115
轻奢々
轻奢々 2020-12-08 13:38

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency?

Collections.sy         


        
6条回答
  •  眼角桃花
    2020-12-08 13:53

    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.

提交回复
热议问题