WeakHashMapK,V


WeakHashMap<K,V> java.util

Java 1.2 collection

This class implements Map using an internal hashtable. It is similar in features and performance to HashMap , except that it uses the capabilities of the java.lang.ref package, so that the key-to-value mappings it maintains do not prevent the key objects from being reclaimed by the garbage collector. When there are no more references to a key object except for the weak reference maintained by the WeakHashMap , the garbage collector reclaims the object, and the WeakHashMap deletes the mapping between the reclaimed key and its associated value. If there are no references to the value object except for the one maintained by the WeakHashMap , the value object also becomes available for garbage collection. Thus, you can use a WeakHashMap to associate an auxiliary value with an object without preventing either the object (the key) or the auxiliary value from being reclaimed. See HashMap for a discussion of the implementation features of this class. See Map for a description of the methods it defines.

WeakHashMap is primarily useful with objects whose equals( ) methods use the == operator for comparison. It is less useful with key objects of type String , for example, because there can be multiple String objects that are equal to one another and, even if the original key value has been reclaimed by the garbage collector, it is always possible to pass a String with the same value to the get( ) method.

Figure 16-69. java.util.WeakHashMap<K,V>

 public class  WeakHashMap<K,V>  extends AbstractMap<K,V> implements Map<K,V> {  // Public Constructors  public  WeakHashMap  ( );        public  WeakHashMap  (int  initialCapacity  );  1.3  public  WeakHashMap  (Map<? extends K,? extends V>  t  );        public  WeakHashMap  (int  initialCapacity  , float  loadFactor  );  // Methods Implementing Map  public void  clear  ( );        public boolean  containsKey  (Object  key  );  1.4  public boolean  containsValue  (Object  value  );        public Set<Map.Entry<K,V>>  entrySet  ( );        public V  get  (Object  key  );        public boolean  isEmpty  ( );  default:true   1.4  public Set<K>  keySet  ( );        public V  put  (K  key  , V  value  );  1.4  public void  putAll  (Map<? extends K,? extends V>  m  );        public V  remove  (Object  key  );        public int  size  ( );  1.4  public Collection<V>  values  ( );   } 



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net