How to use WeakReference in a HashMap?

X

Xavier Tarrago

To retrieve your entry, you have to use as key an object that equals the
original key.
Two answers :
1 - I think that what you are triying to build is already there. It is a
WeakHashMap in jsdk 1.2 and later. (Source code is available)
------------------------------------
A hashtable-based Map implementation with weak keys. An entry in a
WeakHashMap will automatically be removed when its key is no longer in
ordinary use. More precisely, the presence of a mapping for a given key will
not prevent the key from being discarded by the garbage collector, that is,
made finalizable, finalized, and then reclaimed. When a key has been
discarded its entry is effectively removed from the map, so this class
behaves somewhat differently than other Map implementations
------------------------------------

2 - if you persist, you should derive WeakReference to overload equals()
and hash() for your key object. When you use a key (say getkey) ou will get
the right object it if for this entry (entrykey), getkey.hash() ==
entrykey.hash() and (getkey == entrykey || getkey.equals(entrykey)).
 
J

Jacob

I have a caching system keepeing properties for
objects in a HashMap. The objects may go away and
I dont want the cache to block garbage collection.

A natural choice whould be to use WeakReference as
keys:

HashMap map = new HashMap();
String key = "KEY";
map.put (new WeakReference (key), "VALUE");

But neither of these approaches will return me
the value:

h.get ("KEY"); // returns null
h.get (new WeakReference ("KEY")); // returns null

Any ideas?

Thanks!
 
C

Christophe Vanfleteren

Jacob said:
I have a caching system keepeing properties for
objects in a HashMap. The objects may go away and
I dont want the cache to block garbage collection.

A natural choice whould be to use WeakReference as
keys:

HashMap map = new HashMap();
String key = "KEY";
map.put (new WeakReference (key), "VALUE");

But neither of these approaches will return me
the value:

h.get ("KEY"); // returns null
h.get (new WeakReference ("KEY")); // returns null

Any ideas?

Thanks!

Use the WeakHashMap that comes with the JDK:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/WeakHashMap.html
 
S

sks

Jacob said:
I have a caching system keepeing properties for
objects in a HashMap. The objects may go away and
I dont want the cache to block garbage collection.

A natural choice whould be to use WeakReference as
keys:

HashMap map = new HashMap();
String key = "KEY";
map.put (new WeakReference (key), "VALUE");

But neither of these approaches will return me
the value:

h.get ("KEY"); // returns null
h.get (new WeakReference ("KEY")); // returns null

If it's for a memory sensitive cache, ie you want the cache to hold objects
while they have references elsewhere, but once the other strong references
have been cleared you want the cache objects to be eligable for GC if memory
becomes an issue, then you should use a SoftReference for the value.

Object obj = new Object();//object to be cached
String key = "KEY";
map.put(key, new SoftReference(obj));

SoftReference ref = (SoftReference) map.get(key);
obj = ref.get();

!tested
!compiled

If you want to use weak references for the keys, then the objects will be
discarded once the keys are no longer in use elsewhere, which isn't much use
for a cache. This is what you've described above, and you can use
java.util.WeakHashMap to do exactly what you were trying to do with your
code.
 
C

Chris Smith

Jacob said:
A natural choice whould be to use WeakReference as
keys:

HashMap map = new HashMap();
String key = "KEY";
map.put (new WeakReference (key), "VALUE");

But neither of these approaches will return me
the value:

h.get ("KEY"); // returns null
h.get (new WeakReference ("KEY")); // returns null

Any ideas?

Use java.util.WeakHashMap. In fact, this is one of the rare cases where
you actually *want* to use WeakHashMap. Most people who try to use the
class are misunderstanding its functionality. (Specifically, note that
is uses weak keys rather than weak values, which meets your needs just
fine but is counterintuitive to most everyone else.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top