• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

WeakHashMap

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

thanx,
Vaibhav
0
vaibhavmishra062201
Asked:
vaibhavmishra062201
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
riaancorneliusCommented:
0
 
riaancorneliusCommented:
Personally I think it behaves too unpredictably too be of much use.
0
 
zzynxSoftware engineerCommented:
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
MogalManicCommented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now