Questions tagged [weakhashmap]

A special data structure in Java, a WeakHashMap is a hashtable-based Map with weak keys, meaning when a key has been discarded its entry is effectively removed from the map.

Java's WeakHashMap is a hashtable-based Map with weak keys, meaning when a key has been discarded its entry is effectively removed from the map.

This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed.

77 questions
158
votes
8 answers

What is a WeakHashMap and when to use it?

What is a WeakHashMap and when should one be using it? What are the differences between a WeakHashMap and a HashMap?
developer
  • 9,116
  • 29
  • 91
  • 150
58
votes
7 answers

Is there java.util.concurrent equivalent for WeakHashMap?

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap()); i.e. is there something from java.util.concurrent one…
Nikita
  • 6,019
  • 8
  • 45
  • 54
29
votes
1 answer

Set equivalent of WeakHashMap?

Is HashSet> the Set equivalent of WeakHashMap? That is, will entries be automatically deleted when they are no longer referenced? If not, what is the equivalent?
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
22
votes
6 answers

WeakHashMap example

I create a WeakHashMap as WeakHashMap map = new WeakHashMap(); map.put(emp,"hello"); where emp is an Employee object. Now if I do emp = null or say emp object is no longer referenced, then will the entry be removed…
Anand
  • 20,708
  • 48
  • 131
  • 198
15
votes
4 answers

How does a weak hash map know to garbage-collect an object?

I recently found out about the WeakHashMap data structure in Java. However, I don't understand what it means by it garbage-collects a mapping when it is no longer in ordinary use. How does the data structure know I will no longer use a key in my…
John Hoffman
  • 17,857
  • 20
  • 58
  • 81
13
votes
6 answers

WeakHashMap vs HashMap

In the following code example when keys are set to null and System.gc() is called, the WeakHashMap loses all mappings and is emptied. class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 =…
Nishant
  • 1,142
  • 1
  • 9
  • 27
9
votes
1 answer

WeakHashMap and strongly referenced value

Javadocs says "When a key has been discarded its entry is effectively removed from the map". But unless there is another thread that occasionally removes such Map.Entry entries, won't the value objects be strongly referenced by the map? But since…
Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
9
votes
2 answers

Will a WeakHashMap's entry be collected if the value contains the only strong reference to the key?

I need to associate some data with a key for its lifetime, so I am using a WeakHashMap. However, in addition I need to get a key by its corresponding value. The easy way to do it is to hold on to the reference when creating a value: public class Key…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
9
votes
5 answers

Are keySet entries of a WeakHashMap never null?

If I iterate over the key set of a WeakHashMap, do I need to check for null values? WeakHashMap> hm = new WeakHashMap>(); for ( MyObject item : hm.keySet() ) { if ( item !=…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
9
votes
4 answers

Java Weak Hash Map - Need to remove entry based on weakness of value, not key

So the Java WeakHashMap lets one create a map whose entries are removed if its keys become weak. But how can I create a Map whose entries are removed when the values in the map become weak? The reason I want to use a map is as a Global Hash Table…
Binaromong
  • 540
  • 7
  • 26
9
votes
1 answer

How to effectively use a String as a WeakHashMap key in Java, or an alternative solution

I am managing a project in Java that stores user data. Users can be online, or offline. When users are online, their data is loaded into the data object for easy accessibility, and offloaded when they log off. However, for offline users, in the…
CorrieKay
  • 181
  • 1
  • 4
  • 15
8
votes
2 answers

Java: Stack with weak references

In Java there is a data structure called a WeakHashMap that stores weak references as keys. Whenever the weak references are taken out of memory the entry is removed from the map. If I have a data structure such as a Stack or a Set where I am…
7
votes
3 answers

Why WeakHashMap holds strong reference to value after GC?

Key object in WeakHashMap became weakly reachable. And map should be remove the entry after GC. But a strong reference to the value object remains. Why? The same behavior is observed with guava weakkeys map. Expected output: ... refKey.get =…
Rumter
  • 73
  • 4
6
votes
1 answer

How does a value in an entry in the WeakHashMap gets garbage collected when the actual object is garbage collected?

First of all I would like to clarify my understanding of the WeakReference as the following question depends on the same. static void test() { Person p = new Person(); WeakReference person = new WeakReference<>(p); p = null; …
6
votes
1 answer

Guava MapMaker().weakKeys().makeMap() vs WeakHashMap

We have a Scala server that is getting a node tree using Protocol Buffers over a socket and we need to attach additional data to each node. In a single threaded context and when both the node tree and the associated data will have their strong…
Blair Zajac
  • 4,555
  • 3
  • 25
  • 36
1
2 3 4 5 6