|
|||||||||
| Home >> All >> java >> [ util overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.util
Class WeakHashMap

java.lang.Objectjava.util.AbstractMap
java.util.WeakHashMap
- All Implemented Interfaces:
- Map
- public class WeakHashMap
- extends AbstractMap
- implements Map
- extends AbstractMap
A weak hash map has only weak references to the key. This means that it allows the key to be garbage collected if it is not used otherwise. If this happens, the entry will eventually disappear from the map, asynchronously.
A weak hash map makes most sense when the keys doesn't override the
equals method: If there is no other reference to the
key nobody can ever look up the key in this table and so the entry
can be removed. This table also works when the equals
method is overloaded, such as String keys, but you should be prepared
to deal with some entries disappearing spontaneously.
Other strange behaviors to be aware of: The size of this map may
spontaneously shrink (even if you use a synchronized map and synchronize
it); it behaves as if another thread removes entries from this table
without synchronization. The entry set returned by entrySet
has similar phenomenons: The size may spontaneously shrink, or an
entry, that was in the set before, suddenly disappears.
A weak hash map is not meant for caches; use a normal map, with soft references as values instead, or try LinkedHashMap.
The weak hash map supports null values and null keys. The null key is never deleted from the map (except explictly of course). The performance of the methods are similar to that of a hash map.
The value objects are strongly referenced by this table. So if a value object maintains a strong reference to the key (either direct or indirect) the key will never be removed from this map. According to Sun, this problem may be fixed in a future release. It is not possible to do it with the jdk 1.2 reference model, though.
- Since:
- 1.2
| Nested Class Summary | |
private static class |
WeakHashMap.WeakBucket
A bucket is a weak reference to the key, that contains a strong reference to the value, a pointer to the next bucket and its slot number. |
private class |
WeakHashMap.WeakEntrySet
The entry set. |
| Nested classes inherited from class java.util.AbstractMap |
AbstractMap.BasicMapEntry |
| Nested classes inherited from class java.util.Map |
Map.Entry |
| Field Summary | |
(package private) WeakHashMap.WeakBucket[] |
buckets
The hash buckets. |
private static int |
DEFAULT_CAPACITY
The default capacity for an instance of HashMap. |
private static float |
DEFAULT_LOAD_FACTOR
The default load factor of a HashMap. |
private float |
loadFactor
The load factor of this WeakHashMap. |
(package private) int |
modCount
The number of structural modifications. |
(package private) static java.lang.Object |
NULL_KEY
This is used instead of the key value null. |
private java.lang.ref.ReferenceQueue |
queue
The reference queue where our buckets (which are WeakReferences) are registered to. |
(package private) int |
size
The number of entries in this hash map. |
private WeakHashMap.WeakEntrySet |
theEntrySet
The entry set returned by entrySet(). |
private int |
threshold
The rounded product of the capacity (i.e. |
| Fields inherited from class java.util.AbstractMap |
ENTRIES, keys, KEYS, values, VALUES |
| Constructor Summary | |
WeakHashMap()
Creates a new weak hash map with default load factor and default capacity. |
|
WeakHashMap(int initialCapacity)
Creates a new weak hash map with default load factor and the given capacity. |
|
WeakHashMap(int initialCapacity,
float loadFactor)
Creates a new weak hash map with the given initial capacity and load factor. |
|
WeakHashMap(Map m)
Construct a new WeakHashMap with the same mappings as the given map. |
|
| Method Summary | |
(package private) void |
cleanQueue()
Cleans the reference queue. |
void |
clear()
Clears all entries from this map. |
boolean |
containsKey(java.lang.Object key)
Tells if the map contains the given key. |
boolean |
containsValue(java.lang.Object value)
Returns true if the map contains at least one key which points to the specified object as a value. |
Set |
entrySet()
Returns a set representation of the entries in this map. |
java.lang.Object |
get(java.lang.Object key)
Gets the value the key is mapped to. |
private int |
hash(java.lang.Object key)
Simply hashes a non-null Object to its array index. |
private void |
internalAdd(java.lang.Object key,
java.lang.Object value)
Adds a new key/value pair to the hash map. |
private WeakHashMap.WeakBucket.WeakEntry |
internalGet(java.lang.Object key)
Finds the entry corresponding to key. |
(package private) void |
internalRemove(WeakHashMap.WeakBucket bucket)
Removes a bucket from this hash map, if it wasn't removed before (e.g. |
boolean |
isEmpty()
Tells if the map is empty. |
Set |
keySet()
Returns a set representation of the keys in this map. |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Adds a new key/value mapping to this map. |
void |
putAll(Map m)
Puts all of the mappings from the given map into this one. |
private void |
rehash()
Rehashes this hashtable. |
java.lang.Object |
remove(java.lang.Object key)
Removes the key and the corresponding value from this map. |
int |
size()
Returns the size of this hash map. |
Collection |
values()
Returns a collection representation of the values in this map. |
| Methods inherited from class java.util.AbstractMap |
clone, equals, equals, hashCode, hashCode, toString |
| Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Map |
equals, hashCode |
| Field Detail |
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
- The default capacity for an instance of HashMap.
Sun's documentation mildly suggests that this (11) is the correct
value.
- See Also:
- Constant Field Values
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTOR
- The default load factor of a HashMap.
- See Also:
- Constant Field Values
NULL_KEY
static final java.lang.Object NULL_KEY
- This is used instead of the key value null. It is needed
to distinguish between an null key and a removed key.
queue
private final java.lang.ref.ReferenceQueue queue
- The reference queue where our buckets (which are WeakReferences) are
registered to.
size
int size
- The number of entries in this hash map.
loadFactor
private float loadFactor
- The load factor of this WeakHashMap. This is the maximum ratio of
size versus number of buckets. If size grows the number of buckets
must grow, too.
threshold
private int threshold
- The rounded product of the capacity (i.e. number of buckets) and
the load factor. When the number of elements exceeds the
threshold, the HashMap calls
rehash().
modCount
int modCount
- The number of structural modifications. This is used by
iterators, to see if they should fail. This doesn't count
the silent key removals, when a weak reference is cleared
by the garbage collection. Instead the iterators must make
sure to have strong references to the entries they rely on.
theEntrySet
private final WeakHashMap.WeakEntrySet theEntrySet
- The entry set returned by
entrySet().
buckets
WeakHashMap.WeakBucket[] buckets
- The hash buckets. These are linked lists. Package visible for use in
nested classes.
| Constructor Detail |
WeakHashMap
public WeakHashMap()
- Creates a new weak hash map with default load factor and default
capacity.
WeakHashMap
public WeakHashMap(int initialCapacity)
- Creates a new weak hash map with default load factor and the given
capacity.
WeakHashMap
public WeakHashMap(int initialCapacity,
float loadFactor)
- Creates a new weak hash map with the given initial capacity and
load factor.
WeakHashMap
public WeakHashMap(Map m)
- Construct a new WeakHashMap with the same mappings as the given map.
The WeakHashMap has a default load factor of 0.75.
- Since:
- 1.3
| Method Detail |
hash
private int hash(java.lang.Object key)
- Simply hashes a non-null Object to its array index.
cleanQueue
void cleanQueue()
- Cleans the reference queue. This will poll all references (which
are WeakBuckets) from the queue and remove them from this map.
This will not change modCount, even if it modifies the map. The
iterators have to make sure that nothing bad happens.
Currently the iterator maintains a strong reference to the key, so that is no problem.
rehash
private void rehash()
- Rehashes this hashtable. This will be called by the
add()method if the size grows beyond the threshold. It will grow the bucket size at least by factor two and allocates new buckets.
internalGet
private WeakHashMap.WeakBucket.WeakEntry internalGet(java.lang.Object key)
- Finds the entry corresponding to key. Since it returns an Entry
it will also prevent the key from being removed under us.
internalAdd
private void internalAdd(java.lang.Object key, java.lang.Object value)
- Adds a new key/value pair to the hash map.
internalRemove
void internalRemove(WeakHashMap.WeakBucket bucket)
- Removes a bucket from this hash map, if it wasn't removed before
(e.g. one time through rehashing and one time through reference queue).
Package visible for use in nested classes.
size
public int size()
- Returns the size of this hash map. Note that the size() may shrink
spontaneously, if the some of the keys were only weakly reachable.
- Specified by:
sizein interfaceMap- Overrides:
sizein classAbstractMap
isEmpty
public boolean isEmpty()
- Tells if the map is empty. Note that the result may change
spontanously, if all of the keys were only weakly reachable.
- Specified by:
isEmptyin interfaceMap- Overrides:
isEmptyin classAbstractMap
containsKey
public boolean containsKey(java.lang.Object key)
- Tells if the map contains the given key. Note that the result
may change spontanously, if the key was only weakly
reachable.
- Specified by:
containsKeyin interfaceMap- Overrides:
containsKeyin classAbstractMap
get
public java.lang.Object get(java.lang.Object key)
- Gets the value the key is mapped to.
- Specified by:
getin interfaceMap- Overrides:
getin classAbstractMap
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
- Adds a new key/value mapping to this map.
- Specified by:
putin interfaceMap- Overrides:
putin classAbstractMap
remove
public java.lang.Object remove(java.lang.Object key)
- Removes the key and the corresponding value from this map.
- Specified by:
removein interfaceMap- Overrides:
removein classAbstractMap
entrySet
public Set entrySet()
- Returns a set representation of the entries in this map. This
set will not have strong references to the keys, so they can be
silently removed. The returned set has therefore the same
strange behaviour (shrinking size(), disappearing entries) as
this weak hash map.
- Specified by:
entrySetin interfaceMap- Specified by:
entrySetin classAbstractMap
clear
public void clear()
- Clears all entries from this map.
- Specified by:
clearin interfaceMap- Overrides:
clearin classAbstractMap
containsValue
public boolean containsValue(java.lang.Object value)
- Returns true if the map contains at least one key which points to
the specified object as a value. Note that the result
may change spontanously, if its key was only weakly reachable.
- Specified by:
containsValuein interfaceMap- Overrides:
containsValuein classAbstractMap
keySet
public Set keySet()
- Returns a set representation of the keys in this map. This
set will not have strong references to the keys, so they can be
silently removed. The returned set has therefore the same
strange behaviour (shrinking size(), disappearing entries) as
this weak hash map.
- Specified by:
keySetin interfaceMap- Overrides:
keySetin classAbstractMap
putAll
public void putAll(Map m)
- Puts all of the mappings from the given map into this one. If the
key already exists in this map, its value is replaced.
- Specified by:
putAllin interfaceMap- Overrides:
putAllin classAbstractMap
values
public Collection values()
- Returns a collection representation of the values in this map. This
collection will not have strong references to the keys, so mappings
can be silently removed. The returned collection has therefore the same
strange behaviour (shrinking size(), disappearing entries) as
this weak hash map.
- Specified by:
valuesin interfaceMap- Overrides:
valuesin classAbstractMap
|
|||||||||
| Home >> All >> java >> [ util overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC