weakhashmap

Is there java.util.concurrent equivalent for WeakHashMap?

情到浓时终转凉″ 提交于 2019-11-28 06:10:37
Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap<Class, Object>()); i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>())); obviously won't work Steven Schlansker 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

Is WeakHashMap ever-growing, or does it clear out the garbage keys?

梦想与她 提交于 2019-11-28 01:25:03
问题 I am trying to use WeakHashMap as a concurrent Set of weak references. this.subscribers = Collections.synchronizedSet( Collections.newSetFromMap( new WeakHashMap <>() ) ); When an element goes to garbage-collection, my set continues to report it as a part of the collection. So it seems the map is ever-growing. The documentation says: When a key has been discarded its entry is effectively removed from the map,… But that does not seem to be the case in practice. Is there ever a point at which

What is a WeakHashMap and when to use it? [duplicate]

本小妞迷上赌 提交于 2019-11-27 10:03:23
This question already has an answer here: When would you use a WeakHashMap or a WeakReference? 10 answers What is a WeakHashMap and when should one be using it? What are the differences between a WeakHashMap and a HashMap ? vickirk Elements in a weak hashmap can be reclaimed by the garbage collector if there are no other strong references to the key object, this makes them useful for caches/lookup storage. Weak reference are not restricted to these hash tables, you can use WeakReference for single objects. They are useful to save resource, you can keep a reference to something but allow it to

Is there java.util.concurrent equivalent for WeakHashMap?

帅比萌擦擦* 提交于 2019-11-27 01:19:05
问题 Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap<Class, Object>()); i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>())); obviously won't work 回答1: Guava's CacheBuilder class allows you to do this easily. CacheBuilder.newBuilder().weakKeys().build()

What is a WeakHashMap and when to use it? [duplicate]

落爺英雄遲暮 提交于 2019-11-26 22:19:27
问题 This question already has answers here : When would you use a WeakHashMap or a WeakReference? (10 answers) Closed 3 years ago . What is a WeakHashMap and when should one be using it? What are the differences between a WeakHashMap and a HashMap ? 回答1: Elements in a weak hashmap can be reclaimed by the garbage collector if there are no other strong references to the key object, this makes them useful for caches/lookup storage. Weak reference are not restricted to these hash tables, you can use