Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using WeakHashMap in place of HashMap .

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic