How to use WeakReference in a HashMap?

Discussion in 'Java' started by Xavier Tarrago, Jun 18, 2004.

  1. 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!
    >
     
    Xavier Tarrago, Jun 18, 2004
    #1
    1. Advertising

  2. Xavier Tarrago

    Jacob Guest

    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!
     
    Jacob, Jun 18, 2004
    #2
    1. Advertising

  3. 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/api/java/util/WeakHashMap.html

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Jun 18, 2004
    #3
  4. Xavier Tarrago

    sks Guest

    "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.
     
    sks, Jun 18, 2004
    #4
  5. Xavier Tarrago

    Jacob Guest

    Christophe Vanfleteren wrote:

    > Use the WeakHashMap that comes with the JDK:


    Thanks Christophe.

    WeakHashMap is it.
     
    Jacob, Jun 18, 2004
    #5
  6. Xavier Tarrago

    Chris Smith Guest

    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
     
    Chris Smith, Jun 19, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    2
    Views:
    385
    Adam Maass
    Oct 21, 2003
  2. Ken
    Replies:
    15
    Views:
    38,241
    princebadshah
    Mar 5, 2009
  3. Vince Darley
    Replies:
    4
    Views:
    4,470
    emilchacko
    Mar 2, 2010
  4. Rakesh
    Replies:
    10
    Views:
    12,217
    Mike Schilling
    Apr 8, 2008
  5. Paul J. Lucas

    Doing one last thing to a WeakReference

    Paul J. Lucas, Jun 18, 2008, in forum: Java
    Replies:
    70
    Views:
    2,065
    Paul J. Lucas
    Jul 2, 2008
Loading...

Share This Page