M
Mike Schilling
Tegiri said:You do realize that the key is also referenced by the hash map
internals (to be specific, the entry that contains the key is in
some
hash chain.) .Absent some mechanism like weak references, the key
can't be collection unless it's explicitly removed from the map.
Ok, let's be more specific.
class HashMap {
Entry[] table;
...
}
No problem here, I assume if there is no references to HashMap all
the
rederences to Entry would be marked for deletion.
class Entry<K,V> {
K key;
V value;
Entry<K,V> next;
}
Next if the Entry object is marked for deletion, then key, value,
and
the next entry in the chain are maked as well.
So, there is nothing specific about the key at all!
What you're saying is correct, but doesn't describe the situatioin
where WeakHashMaps are used. Suppose the HashMap is the only
remaining reference to a key value, and you don't want the fact that
the key is in the map to keep it and its associated value in memory.
That is, you'd like iterating over the map to return only entries that
are currently referenced by something other than the map.. Normal
HashMaps, which have hard references to their keys and values, keep
all of them in memory regardless of whether they're used in any other
way. WeakHashMaps do not.