jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes WeakHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "WeakHashMap" Watch "WeakHashMap" New topic


Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
Can anyone explain when WeakHashMap is used and how it works?? I'm studying it here but I didn't understand what's used for...
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Items in a WeakHashMap are eligible for garbage collection if the WeakHashMap has the only reference.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I wrote this some time ago:

SCJP2. Please Indent your code using UBB Code
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
I think items (keys and values) in a WeakHashMap are removed from the map when the key is no longer referenced outside the map. The value then eligible for garbage collection only if it is not referenced elsewhere (perhaps by another key in the map).
I've not had an occasion in which I knew needed WeakHashMap yet, so there is a chance I don't have this right.
I suspect WeakHashMap may be useful when a subsystem needs to provide a handle by which other subsystems can refer to an object. The handle could be key in a WeakHashMap. When no other subsystem (and nothing besides the WeakHashMap) contains a reference to a particular key, that key and its value are removed from the map. At that point, if nothing else refers to the value object, the value object is unreferenced and hence eligable for garbage collection.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
me too

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: WeakHashMap
jQuery in Action, 3rd edition