Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to use WeakReference in a HashMap?

Reply
Thread Tools

How to use WeakReference in a HashMap?

 
 
Xavier Tarrago
Guest
Posts: n/a
 
      06-18-2004
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)).

"Jacob" <> a écrit dans le message de
news:...
> 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!
>



 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      06-18-2004
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!

 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      06-18-2004
Jacob wrote:

> 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/...akHashMap.html

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
sks
Guest
Posts: n/a
 
      06-18-2004

"Jacob" <> wrote in message
news:...
> 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.


 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      06-18-2004
Christophe Vanfleteren wrote:

> Use the WeakHashMap that comes with the JDK:


Thanks Christophe.

WeakHashMap is it.

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-19-2004
Jacob wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doing one last thing to a WeakReference Paul J. Lucas Java 70 07-02-2008 04:48 AM
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM
Detecting when a WeakReference gets cleared. Dave Rudolf Java 2 10-21-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57