SCJP2. Please Indent your code using UBB Code
"I'm not back." - Bill Harding, Twister
SCJP2. Please Indent your code using UBB Code
Originally posted by Jose Botella:
"[...] With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing."
What sort of confussion is this?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
It often isn't - which is exactly why a WeakHashMap can be useful. You might think, why can't you realise the association with an instance field on the key object? Well, that's not always desirable or possible - the objects may come from a library, for instance.Originally posted by Jose Botella:
Why it would be needed to create several copies of an object? I mean more than one copy.
A WeakReference to what? To the key? - That's an entirely different behaviour, with a WeakHashMap the key is subject to the normal garbage collection rules. To the value? - that would allow the value to be garbage collected at any time, while with a WeakHashMap it can be garbage collected only if you lost all your references to the key.is Bruce referring really to the value stored in a WeakHashMap? why not using a simple WeakReference object?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
You might think, why can't you realise the association with an instance field on the key object? Well, that's not always desirable or possible - the objects may come from a library, for instance.
A WeakReference to what? To the key? - That's an entirely different behaviour, with a WeakHashMap the key is subject to the normal garbage collection rules. To the value? - that would allow the value to be garbage collected at any time, while with a WeakHashMap it can be garbage collected only if you lost all your references to the key.
SCJP2. Please Indent your code using UBB Code
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|