org.apache.commons.collections

Class ReferenceMap

public class ReferenceMap extends AbstractMap

Deprecated: Moved to map subpackage. Due to be removed in v4.0.

Hash-based 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 java.util.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 java.util.Collections#synchronizedMap to provide synchronized access to a ReferenceMap.

Since: Commons Collections 2.1

Version: $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $

Author: Paul Jack

See Also: java.lang.ref.Reference

Field Summary
static intHARD
Constant indicating that hard references should be used.
static intSOFT
Constant indicating that soft references should be used.
static intWEAK
Constant indicating that weak references should be used.
Constructor Summary
ReferenceMap()
Constructs a new ReferenceMap that will use hard references to keys and soft references to values.
ReferenceMap(int keyType, int valueType, boolean purgeValues)
Constructs a new ReferenceMap that will use the specified types of references.
ReferenceMap(int keyType, int valueType)
Constructs a new ReferenceMap that will use the specified types of references.
ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues)
Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.
ReferenceMap(int keyType, int valueType, int capacity, float loadFactor)
Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.
Method Summary
voidclear()
Clears this map.
booleancontainsKey(Object key)
Returns true if this map contains the given key.
SetentrySet()
Returns a set view of this map's entries.
Objectget(Object key)
Returns the value associated with the given key, if any.
booleanisEmpty()
Returns true if this map is empty.
SetkeySet()
Returns a set view of this map's keys.
Objectput(Object key, Object value)
Associates the given key with the given value.

Neither the key nor the value may be null.

Objectremove(Object key)
Removes the key and its associated value from this map.
intsize()
Returns the size of this map.
Collectionvalues()
Returns a collection view of this map's values.

Field Detail

HARD

public static final int HARD
Constant indicating that hard references should be used.

SOFT

public static final int SOFT
Constant indicating that soft references should be used.

WEAK

public static final int WEAK
Constant indicating that weak references should be used.

Constructor Detail

ReferenceMap

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

ReferenceMap

public ReferenceMap(int keyType, int valueType, boolean purgeValues)
Constructs a new ReferenceMap that will use the specified types of references.

Parameters: keyType the type of reference to use for keys; must be HARD, SOFT, WEAK valueType the type of reference to use for values; must be HARD, SOFT, WEAK purgeValues should the value be automatically purged when the key is garbage collected

ReferenceMap

public ReferenceMap(int keyType, int valueType)
Constructs a new ReferenceMap that will use the specified types of references.

Parameters: keyType the type of reference to use for keys; must be HARD, SOFT, WEAK valueType the type of reference to use for values; must be HARD, SOFT, WEAK

ReferenceMap

public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues)
Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

Parameters: keyType the type of reference to use for keys; must be HARD, SOFT, WEAK valueType the type of reference to use for values; must be HARD, SOFT, WEAK capacity the initial capacity for the map loadFactor the load factor for the map purgeValues should the value be automatically purged when the key is garbage collected

ReferenceMap

public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor)
Constructs a new ReferenceMap with the specified reference types, load factor and initial capacity.

Parameters: keyType the type of reference to use for keys; must be HARD, SOFT, WEAK valueType the type of reference to use for values; must be HARD, SOFT, WEAK capacity the initial capacity for the map loadFactor the load factor for the map

Method Detail

clear

public void clear()
Clears this map.

containsKey

public boolean containsKey(Object key)
Returns true if this map contains the given key.

Returns: true if the given key is in this map

entrySet

public Set entrySet()
Returns a set view of this map's entries.

Returns: a set view of this map's entries

get

public Object get(Object key)
Returns the value associated with the given key, if any.

Returns: the value associated with the given key, or null if the key maps to no value

isEmpty

public boolean isEmpty()
Returns true if this map is empty.

Returns: true if this map is empty

keySet

public Set keySet()
Returns a set view of this map's keys.

Returns: a set view of this map's keys

put

public Object put(Object key, Object value)
Associates the given key with the given value.

Neither the key nor the value may be null.

Parameters: key the key of the mapping value 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

public Object remove(Object key)
Removes the key and its associated value from this map.

Parameters: key the key to remove

Returns: the value associated with that key, or null if the key was not in the map

size

public int size()
Returns the size of this map.

Returns: the size of this map

values

public Collection values()
Returns a collection view of this map's values.

Returns: a collection view of this map's values.

Copyright © 2001-2008 Apache Software Foundation. All Rights Reserved.