How does weakhashmap work?

How and how does weakhashmap understand that a reference to one of its keys is outdated now, especially if the key is a String that is concatenated?

+7
java hashmap
source share
2 answers

You should not use string literals with WeakHashMap (well you can, but that wouldn't make sense):

String myKey = "somekey"; 

instead you should use:

 String myKey = new String("somekey"); 

In the latter case, String is not combined.

+3
source share

The word "obsolete" is inaccurate. The condition in question is garbage collection. The value is removed from WeakHashMap when and if the key is collected from garbage. Period.

+1
source share

All Articles