|
JavaTM 2 Platform Std. Ed. v1.3.1 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.AbstractMap | +--java.util.WeakHashMap
A hashtable-based Map
implementation with weak keys.
An entry in a WeakHashMap
will automatically be removed when
its key is no longer in ordinary use. More precisely, the presence of a
mapping for a given key will not prevent the key from being discarded by the
garbage collector, that is, made finalizable, finalized, and then reclaimed.
When a key has been discarded its entry is effectively removed from the map,
so this class behaves somewhat differently than other Map
implementations.
Both null values and the null key are supported. This class has
performance characteristics similar to those of the HashMap
class, and has the same efficiency parameters of initial capacity
and load factor.
Like most collection classes, this class is not synchronized. A
synchronized WeakHashMap
may be constructed using the
Collections.synchronizedMap
method.
This class is intended primarily for use with key objects whose
equals
methods test for object identity using the
==
operator. Once such a key is discarded it can never be
recreated, so it is impossible to do a lookup of that key in a
WeakHashMap
at some later time and be surprised that its entry
has been removed. This class will work perfectly well with key objects
whose equals
methods are not based upon object identity, such
as String
instances. With such recreatable key objects,
however, the automatic removal of WeakHashMap
entries whose
keys have been discarded may prove to be confusing.
The behavior of the WeakHashMap
class depends in part upon
the actions of the garbage collector, so several familiar (though not
required) Map
invariants do not hold for this class. Because
the garbage collector may discard keys at any time, a
WeakHashMap
may behave as though an unknown thread is silently
removing entries. In particular, even if you synchronize on a
WeakHashMap
instance and invoke none of its mutator methods, it
is possible for the size
method to return smaller values over
time, for the isEmpty
method to return false
and
then true
, for the containsKey
method to return
true
and later false
for a given key, for the
get
method to return a value for a given key but later return
null
, for the put
method to return
null
and the remove
method to return
false
for a key that previously appeared to be in the map, and
for successive examinations of the key set, the value set, and the entry set
to yield successively smaller numbers of elements.
Each key object in a WeakHashMap
is stored indirectly as
the referent of a weak reference. Therefore a key will automatically be
removed only after the weak references to it, both inside and outside of the
map, have been cleared by the garbage collector.
Implementation note: The value objects in a
WeakHashMap
are held by ordinary strong references. Thus care
should be taken to ensure that value objects do not strongly refer to their
own keys, either directly or indirectly, since that will prevent the keys
from being discarded. Note that a value object may refer indirectly to its
key via the WeakHashMap
itself; that is, a value object may
strongly refer to some other key object whose associated value object, in
turn, strongly refers to the key of the first value object. This problem
may be fixed in a future release.
HashMap
,
WeakReference
Inner classes inherited from class java.util.Map |
Map.Entry |
Constructor Summary | |
WeakHashMap()
Constructs a new, empty WeakHashMap with the default
initial capacity and the default load factor, which is
0.75 . |
|
WeakHashMap(int initialCapacity)
Constructs a new, empty WeakHashMap with the given
initial capacity and the default load factor, which is
0.75 . |
|
WeakHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty WeakHashMap with the given
initial capacity and the given load factor. |
|
WeakHashMap(Map t)
Constructs a new WeakHashMap with the same mappings as the
specified Map. |
Method Summary | |
void |
clear()
Removes all mappings from this map. |
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the
specified key. |
Set |
entrySet()
Returns a Set view of the mappings in this map. |
Object |
get(Object key)
Returns the value to which this map maps the specified key . |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
Object |
put(Object key,
Object value)
Updates this map so that the given key maps to the given
value . |
Object |
remove(Object key)
Removes the mapping for the given key from this map, if
present. |
int |
size()
Returns the number of key-value mappings in this map. |
Methods inherited from class java.util.AbstractMap |
containsValue, equals, hashCode, keySet, putAll, toString, values |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
containsValue, equals, hashCode, keySet, putAll, values |
Constructor Detail |
public WeakHashMap(int initialCapacity, float loadFactor)
WeakHashMap
with the given
initial capacity and the given load factor.initialCapacity
- The initial capacity of the
WeakHashMap
loadFactor
- The load factor of the WeakHashMap
IllegalArgumentException
- If the initial capacity is less than
zero, or if the load factor is
nonpositivepublic WeakHashMap(int initialCapacity)
WeakHashMap
with the given
initial capacity and the default load factor, which is
0.75
.initialCapacity
- The initial capacity of the
WeakHashMap
IllegalArgumentException
- If the initial capacity is less than
zeropublic WeakHashMap()
WeakHashMap
with the default
initial capacity and the default load factor, which is
0.75
.public WeakHashMap(Map t)
WeakHashMap
with the same mappings as the
specified Map. The WeakHashMap
is created with an
initial capacity of twice the number of mappings in the specified map
or 11 (whichever is greater), and a default load factor, which is
0.75.t
- the map whose mappings are to be placed in this map.Method Detail |
public int size()
Map
interface, the time required by this operation is
linear in the size of the map.size
in interface Map
size
in class AbstractMap
java.util.Map
public boolean isEmpty()
true
if this map contains no key-value mappings.isEmpty
in interface Map
isEmpty
in class AbstractMap
java.util.Map
public boolean containsKey(Object key)
true
if this map contains a mapping for the
specified key.containsKey
in interface Map
containsKey
in class AbstractMap
key
- The key whose presence in this map is to be testedpublic Object get(Object key)
key
.
If this map does not contain a value for this key, then return
null
.get
in interface Map
get
in class AbstractMap
key
- The key whose associated value, if any, is to be returnedpublic Object put(Object key, Object value)
key
maps to the given
value
. If the map previously contained a mapping for
key
then that mapping is replaced and the previous value is
returned.put
in interface Map
put
in class AbstractMap
key
- The key that is to be mapped to the given
value
value
- The value to which the given key
is to be
mappednull
if if there was no mapping for the keypublic Object remove(Object key)
key
from this map, if
present.remove
in interface Map
remove
in class AbstractMap
key
- The key whose mapping is to be removednull
if
there was no mapping for the keypublic void clear()
clear
in interface Map
clear
in class AbstractMap
java.util.Map
UnsupportedOperationException
- clear is not supported by this
map.public Set entrySet()
Set
view of the mappings in this map.entrySet
in interface Map
entrySet
in class AbstractMap
java.util.Map
|
JavaTM 2 Platform Std. Ed. v1.3.1 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Java, Java 2D, and JDBC are trademarks or registered trademarks of Oracle and/or its affiliates, in the US and other countries.
Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.