• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util package  RSS feed

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) What is the difference between Hashtable, HashMap and HashSet?
2) What is a WeakHashMap and why is it used?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) What is the difference between Hashtable, HashMap and HashSet?
Both HashMap and Hashtable associate (map) keys to values. HashMap accepts null keys and values, Hashtable does not. Hashtable is synchronized (thread safe), HashMap and HashSet are not. A HashSet contains only values (no mapping) with no duplicate values and like HashMap accepts nulls.
2) What is a WeakHashMap and why is it used?
A WeakHashMap has keys that are weak references. What that means is, once the garbage collector determines that there are no reachable references, other than weak references to the key, then the key and its associated value are effectively removed from the Map. WeakHashMap, like HashMap accepts null keys and values and is not synchronized. It is used when you want the garbage collector to reclaim any object that is only referenced in the Map. It is very easy to cause a memory leak by forgetting that you have an object reference stored somewhere in a collection, the WeakHashMap insures that that won't happen.
[ May 21, 2003: Message edited by: Michael Morris ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!