Class ReferenceMap
- All Implemented Interfaces:
Map
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.
The algorithms used are basically the same as those
in HashMap. In particular, you
can specify a load factor and capacity to suit your
needs. All optional Map operations are
supported.
However, this Map implementation does not
allow null elements. Attempting to add a null key or
or a null value to the map will raise a
NullPointerException.
As usual, this implementation is not synchronized. You
can use Collections.synchronizedMap(java.util.Map<K, V>) to
provide synchronized access to a ReferenceMap.
- Since:
- Commons Collections 2.1
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Paul Jack
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDeprecated.Constant indicating that hard references should be used.static final intDeprecated.Constant indicating that soft references should be used.static final intDeprecated.Constant indicating that weak references should be used. -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Constructs a newReferenceMapthat will use hard references to keys and soft references to values.ReferenceMap(int keyType, int valueType) Deprecated.Constructs a newReferenceMapthat will use the specified types of references.ReferenceMap(int keyType, int valueType, boolean purgeValues) Deprecated.Constructs a newReferenceMapthat will use the specified types of references.ReferenceMap(int keyType, int valueType, int capacity, float loadFactor) Deprecated.Constructs a newReferenceMapwith the specified reference types, load factor and initial capacity.ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues) Deprecated.Constructs a newReferenceMapwith the specified reference types, load factor and initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Deprecated.Clears this map.booleancontainsKey(Object key) Deprecated.Returnstrueif this map contains the given key.entrySet()Deprecated.Returns a set view of this map's entries.Deprecated.Returns the value associated with the given key, if any.booleanisEmpty()Deprecated.Returnstrueif this map is empty.keySet()Deprecated.Returns a set view of this map's keys.Deprecated.Associates the given key with the given value.Deprecated.Removes the key and its associated value from this map.intsize()Deprecated.Returns the size of this map.values()Deprecated.Returns a collection view of this map's values.Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, putAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
HARD
public static final int HARDDeprecated.Constant indicating that hard references should be used.- See Also:
-
SOFT
public static final int SOFTDeprecated.Constant indicating that soft references should be used.- See Also:
-
WEAK
public static final int WEAKDeprecated.Constant indicating that weak references should be used.- See Also:
-
-
Constructor Details
-
ReferenceMap
public ReferenceMap()Deprecated.Constructs a newReferenceMapthat will use hard references to keys and soft references to values. -
ReferenceMap
public ReferenceMap(int keyType, int valueType, boolean purgeValues) Deprecated.Constructs a newReferenceMapthat will use the specified types of references. -
ReferenceMap
public ReferenceMap(int keyType, int valueType) Deprecated.Constructs a newReferenceMapthat will use the specified types of references. -
ReferenceMap
public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues) Deprecated.Constructs a newReferenceMapwith the specified reference types, load factor and initial capacity.- Parameters:
keyType- the type of reference to use for keys; must beHARD,SOFT,WEAKvalueType- the type of reference to use for values; must beHARD,SOFT,WEAKcapacity- the initial capacity for the maploadFactor- the load factor for the mappurgeValues- should the value be automatically purged when the key is garbage collected
-
ReferenceMap
public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor) Deprecated.Constructs a newReferenceMapwith the specified reference types, load factor and initial capacity.
-
-
Method Details
-
size
public int size()Deprecated.Returns the size of this map.- Specified by:
sizein interfaceMap- Overrides:
sizein classAbstractMap- Returns:
- the size of this map
-
isEmpty
public boolean isEmpty()Deprecated.Returnstrueif this map is empty.- Specified by:
isEmptyin interfaceMap- Overrides:
isEmptyin classAbstractMap- Returns:
trueif this map is empty
-
containsKey
Deprecated.Returnstrueif this map contains the given key.- Specified by:
containsKeyin interfaceMap- Overrides:
containsKeyin classAbstractMap- Returns:
- true if the given key is in this map
-
get
Deprecated.Returns the value associated with the given key, if any.- Specified by:
getin interfaceMap- Overrides:
getin classAbstractMap- Returns:
- the value associated with the given key, or
nullif the key maps to no value
-
put
Deprecated.Associates the given key with the given value.Neither the key nor the value may be null.
- Specified by:
putin interfaceMap- Overrides:
putin classAbstractMap- Parameters:
key- the key of the mappingvalue- the value of the mapping- Returns:
- the last value associated with that key, or null if no value was associated with the key
- Throws:
NullPointerException- if either the key or value is null
-
remove
Deprecated.Removes the key and its associated value from this map.- Specified by:
removein interfaceMap- Overrides:
removein classAbstractMap- Parameters:
key- the key to remove- Returns:
- the value associated with that key, or null if the key was not in the map
-
clear
public void clear()Deprecated.Clears this map.- Specified by:
clearin interfaceMap- Overrides:
clearin classAbstractMap
-
entrySet
Deprecated.Returns a set view of this map's entries.- Specified by:
entrySetin interfaceMap- Specified by:
entrySetin classAbstractMap- Returns:
- a set view of this map's entries
-
keySet
Deprecated.Returns a set view of this map's keys.- Specified by:
keySetin interfaceMap- Overrides:
keySetin classAbstractMap- Returns:
- a set view of this map's keys
-
values
Deprecated.Returns a collection view of this map's values.- Specified by:
valuesin interfaceMap- Overrides:
valuesin classAbstractMap- Returns:
- a collection view of this map's values.
-