• Post Reply Bookmark Topic Watch Topic
  • New Topic

WeakHashMap  RSS feed

 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anyone explain what is WeakHashMap and how it is different from ordinary HashMap ?


Regards,
Khuzema
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Official JavaDoc for WeakHashMap does a pretty good job of explaining it I think. Assuming you've already read it, was there something particular you are still unsure of?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A WeakHashMap holds weak references to the keys. That means that when the rest of your application (outside the map) no longer has references to objects that are used as keys in your map, then the map won't prevent the keys from being garbage collected.

"Why would you want to have a Map that can randomly lose entries?", one might ask. WeakHashMap is great for maintaining extra information about objects, as long as those objects are still around.
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!