File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using WeakHashMap in place of HashMap . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using WeakHashMap in place of HashMap ." Watch "Using WeakHashMap in place of HashMap ." New topic

Using WeakHashMap in place of HashMap .

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

In any of the projects till now i have seen , i never saw developers using WeakHashMap to store data in place of a Hashmap .

Please let me know why doesn't peole prefer weakHashMap and save memory , rather than using HashMap

Actually this is the question i want to ask
when to use WeakHashMap for storing Data ??

Thanks in advance .

Save India From Corruption - Anna Hazare.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

The Javadocs for WeakHashMap explain when it's appropriate to use - e.g. when you're using keys of a class that overrides the equals() method you can get unexpected behaviour. And that probably covers the majority of uses of Maps. Of those cases where it would be appropriate, it often won't make much difference.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

May be I didn't get you what you mentioned .
But when we ever we are storing an Object in a Hash Related class , i think that we need to override equals and hashcode methods .

Could you please tell , what you were exactly refering to ??

Thanks in advance .

Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
What do the equals and hashcode methods have to do with when you might use a WeakHashMap over a HashMap?

The WeakHashMap javadocs Matthew mentioned have a lengthy explanation; it would appear that you have not read it so far - that's where you should start.
I agree. Here's the link:
subject: Using WeakHashMap in place of HashMap .
It's not a secret anymore!