Class AbstractReferenceMap<K,​V>

  • Type Parameters:
    K - the type of the keys in this map
    V - the type of the values in this map
    All Implemented Interfaces:
    Map<K,​V>, Get<K,​V>, IterableGet<K,​V>, IterableMap<K,​V>, Put<K,​V>
    Direct Known Subclasses:
    ReferenceIdentityMap, ReferenceMap

    public abstract class AbstractReferenceMap<K,​V>
    extends AbstractHashedMap<K,​V>
    An abstract implementation of a hash-based map that allows the entries to be removed by the garbage collector.

    This class implements all the features necessary for a subclass reference hash-based map. Key-value entries are stored in instances of the ReferenceEntry class which can be overridden and replaced. The iterators can similarly be replaced, without the need to replace the KeySet, EntrySet and Values view classes.

    Overridable methods are provided to change the default hashing behaviour, and to change how entries are added to and removed from the map. Hopefully, all you need for unusual subclasses is here.

    When you construct an AbstractReferenceMap, you can specify what kind of references are used to store the map's keys and values. If non-hard references are used, then the garbage collector can remove mappings if a key or value becomes unreachable, or if the JVM's memory is running low. For information on how the different reference types behave, see Reference.

    Different types of references can be specified for keys and values. The keys can be configured to be weak but the values hard, in which case this class will behave like a WeakHashMap. However, you can also specify hard keys and weak values, or any other combination. The default constructor uses hard keys and soft values, providing a memory-sensitive cache.

    This Map implementation does not allow null elements. Attempting to add a null key or value to the map will raise a NullPointerException.

    All the available iterators can be reset back to the start by casting to ResettableIterator and calling reset().

    This implementation is not synchronized. You can use Collections.synchronizedMap(java.util.Map<K, V>) to provide synchronized access to a ReferenceMap.

    Since:
    3.1 (extracted from ReferenceMap in 3.0)
    See Also:
    Reference