WeakHashMap

Can anybody tell me what is the utility of WeakHashMap and what sought of problems can be solved by using it ?

thanx,
Vaibhav
vaibhavmishra062201Asked:
Who is Participating?
 
riaancorneliusCommented:
0
 
riaancorneliusCommented:
Personally I think it behaves too unpredictably too be of much use.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
vaibhavmishra062201Author Commented:
riaancornelius, i already have jdk docs with me, thanx neway

Thanx zzynx, the article was quite extensive,
btw have you used WeakHashMap anytime or do you know anybody who used this class in real life ?
0
 
zzynxSoftware engineerCommented:
>> btw have you used WeakHashMap anytime or do you know anybody who used this class in real life ?
No. Twice.
Up to you if that means something ;°)
0
 
vaibhavmishra062201Author Commented:
Can you plz post an example ?
0
 
MogalManicConnect With a Mentor Commented:
Here is one:
/**
 * Simple Cache using WeakHashMap
 */
public abstract class Cache
{
    WeakHashMap cacheMap=new WeakHashMap();
   
    /**
     * Return the cached or actual resource requested
     * @param key - key used to obtain resource
     * @return
     */
    public Object get(Object key)
    {
        Object resource=cacheMap.get(key);
        if (resource==null) {
            resource=getActualResource(key);
            cacheMap.put(key, resource);
        }
        return resource;
    }
   
    /**
     * Return the NON-cached resource.  This resource can be a File, Database, calculation or any object that may take time to retrieve.
     * @param key - key used to obtain resource
     * @return
     */
    abstract Object getActualResource(Object key);

}


This class can be used to implement a cache.  The cache will contain objects as long as there is free memory in the VM's heap.  When the garbage collector needs memory the entries will be removed from the cache.  In otherwords, this cache will NOT fill up memory, but only be effective when there is free memory available.
0
 
vaibhavmishra062201Author Commented:
thanx MogalManic
0
 
zzynxSoftware engineerCommented:
Thanks for accepting.

For my interest: Could you tell me what you missed in my comment for not awarding it with an A?
For your interest: You can always ask for more explanation if you're not completely satisfied with a comment.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.