people use it to implement 'cache memory'.
If you have some objects that are reused
often in your application, and their
construction is expensive, and there
are too many of them to keep them all
in memory -
- you use WeakHashMap.
Put there the object which is not currently
used. When this object is needed - get it out
of the map. For most of the time most of those
objects will be staying in the map.
The trick is that they are not held
directly, but through WeakReferences.
So if it gets really 'crowded', when we are running
out of memory, gc will be allowed to collect
them.
So every time you try to get the object out of
the WeakHashMap, you have to ensure it
is still there. Otherwise you need to
recreate it.