Top Description Fields Constructors Methods
org.apache.commons.collections4.map

public Class ReferenceMap<K, V>

extends AbstractReferenceMap<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<K>
the type of the keys in the map
<V>
the type of the values in the map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable

A Map implementation that allows mappings to be removed by the garbage collector.

When you construct a ReferenceMap, 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 is similar to ReferenceIdentityMap. It differs in that keys and values in this class are compared using equals().

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

This implementation is not synchronized. You can use java.util.Collections#synchronizedMap to provide synchronized access to a ReferenceMap. Remember that synchronization will not stop the garbage collector removing entries.

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

Note that ReferenceMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap. This class may throw exceptions when accessed by concurrent threads without synchronization.

Note

As from Commons Collections 3.1 this map extends AbstractReferenceMap (previously it extended AbstractMap). As a result, the implementation is now extensible and provides a MapIterator.

Since
3.0 (previously in main package v2.1)
See Also
java.lang.ref.Reference

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Serialization version

Constructor Summary

AccessConstructor and Description
public
ReferenceMap()

Constructs a new ReferenceMap that will use hard references to keys and soft references to values.

public
ReferenceMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
)

Constructs a new ReferenceMap that will use the specified types of references.

public
ReferenceMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final boolean
should the value be automatically purged when the key is garbage collected
purgeValues
)

Constructs a new ReferenceMap that will use the specified types of references.

public
ReferenceMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final int
the initial capacity for the map
capacity
,
final float
the load factor for the map
loadFactor
)

Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

public
ReferenceMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final int
the initial capacity for the map
capacity
,
final float
the load factor for the map
loadFactor
,
final boolean
should the value be automatically purged when the key is garbage collected
purgeValues
)

Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

Method Summary

Modifier and TypeMethod and Description
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from org.apache.commons.collections4.map.AbstractReferenceMap:
clearcontainsKeycontainsValuecreateEntrycreateEntrySetIteratorcreateKeySetIteratorcreateValuesIteratordoReadObjectdoWriteObjectentrySetgetgetEntryhashEntryinitisEmptyisEqualKeyisKeyTypeisValueTypekeySetmapIteratorpurgepurgepurgeBeforeReadpurgeBeforeWriteputremovesizevalues

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

ReferenceMapback to summary
public ReferenceMap()

Constructs a new ReferenceMap that will use hard references to keys and soft references to values.

ReferenceMapback to summary
public ReferenceMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType)

Constructs a new ReferenceMap that will use the specified types of references.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

ReferenceMapback to summary
public ReferenceMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final boolean purgeValues)

Constructs a new ReferenceMap that will use the specified types of references.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

purgeValues:boolean

should the value be automatically purged when the key is garbage collected

ReferenceMapback to summary
public ReferenceMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final int capacity, final float loadFactor)

Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

capacity:int

the initial capacity for the map

loadFactor:float

the load factor for the map

ReferenceMapback to summary
public ReferenceMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final int capacity, final float loadFactor, final boolean purgeValues)

Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

capacity:int

the initial capacity for the map

loadFactor:float

the load factor for the map

purgeValues:boolean

should the value be automatically purged when the key is garbage collected

Method Detail

readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the map in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream