Hi all,
I think this issue deserves an email, apart from javadoc updates.
How many of you can find usages of softKeys() in your code?
About these usages:
1. Do you rely on SoftReference<K> to keep the keys you want to do lookups in the map?
2. Do you rely on iterating keySet() or entrySet() for discovering K references?
If you answer both negatively, then I think it's fair to say "please treat these softKeys() occurrences as bugs to be fixed". You may want softValues() instead, if you want soft references at all (and not a strong, bounded cache of some sort).
In a nutshell, the issue with softKeys() is this:
For weak/soft keys, to do a successful lookup, you need identity equality, that is, to pass the exact same reference to K you stored in the map in the first place. Which brings up the question, how did you find that K reference?
1. If you had stored it somewhere in a strong fashion, then having softKeys doesn't make sense - you keep strong references to the keys, so they can't be collected when memory is short.
2. If you had stored them in a WeakReference, then softKeys() doesn't provide you anything more than weakKeys(). As soon as your WeakReferences are cleared, there is no way to access the softKeys(), thus they sit around uselessly (with the exceptions mentioned above).
3. If you had stored them in a SoftReference, then it actually makes sense. As soon as you lose your references, so you can no longer do lookups with them, the entry of the map is released as well. (We would be really, really curious to see if any user actually did this, ever!)
As a reminder, another pitfall using softKeys()/weakKeys() is that it's easy to forget that lookups only succeed via identity match, not equality. This, coupled with a computing map, can cause hard to spot performance bugs, since the computing map always hides the lookup failure (by recreating a value).
Dimitris