Class WeakValuesMap

  extended byjava.util.AbstractMap
      extended bymandala.util.WeakValuesMap
All Implemented Interfaces:

public class WeakValuesMap
extends java.util.AbstractMap

A map implementation with weak values.

An entry in a WeakValuesMap will automatically be removed when its value is no longer in ordinary use. More precisely, the presence of a mapping for a given value will not prevent the value from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a value has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

Both null values and the null key are supported.

This class is intended primarily for use with value objects whose equals methods test for object identity using the == operator.

The behavior of the WeakValuesMap class depends in part upon the actions of the garbage collector, so several familiar (though not required) Map invariants do not hold for this class. Because the garbage collector may discard values at any time, a WeakValuesMap may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a WeakValuesMap instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the containsValue method to return true and later false for a given value, for the get method to return a value for a given key but later return null, for the put method to return null and the remove method to return false for a key that previously appeared to be in the map, and for successive examinations of the key set, the value set, and the entry set to yield successively smaller numbers of elements.

Each value object in a WeakValuesMap is stored indirectly as the referent of a weak reference. Therefore a value will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.

This class implementation has been highly inspired on the SoftHashMap implementation by Dr. Heinz Kabutz.


Nested Class Summary
Nested classes inherited from class java.util.Map
Constructor Summary
WeakValuesMap(java.util.Map map)
Method Summary
 void clear()
 java.util.Set entrySet()
 java.lang.Object get(java.lang.Object key)
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Here we put the key, value pair into the Map using a WeakValue object.
 java.lang.Object remove(java.lang.Object key)
 int size()
Methods inherited from class java.util.AbstractMap
clone, containsKey, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait

Constructor Detail


public WeakValuesMap()


public WeakValuesMap(java.util.Map map)
Method Detail


public java.lang.Object get(java.lang.Object key)


public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Here we put the key, value pair into the Map using a WeakValue object.


public java.lang.Object remove(java.lang.Object key)


public void clear()


public int size()


public java.util.Set entrySet()

Mandala help mailing list