public final class WeakMap
extends java.lang.Object
implements java.util.Map
WeakReference
.
Like HashMap
, this implementation provides all of the
optional map operations, and permits the null
key.
Also like HashMap
, this implementation is not synchronized.
If multiple threads share an instance, and at least one of them executes any
modifying operations on the WeakMap
, they have to use external
synchronization.
Unlike other map implementations, WeakMap
is asymmetric in
that put
expects the given value to be a plain object that is
then wrapped in a WeakReference
, while the occurrences of values
in all other methods (containsValue
, entrySet
,
equals
, get
, hashCode
,
remove
, values
, and also the return value of
put
) expect already wrapped instances of
WeakReference
. That is, after weakMap.put("key",
o)
, weakMap.get("key").equals(o)
does not work as
naïvely expected; neither does
weakMap1.putAll(weakMap2)
.
At an arbitrary time after the WeakReference
value of an
entry has been cleared by the garbage collector, the entry is automatically
removed from the map.
Values placed into a WeakMap
may optionally support the
DisposeNotifier
interface. For those that do, the associated
WeakReference
wrappers are automatically cleared as soon as the
values are disposed.
Constructor and Description |
---|
WeakMap()
Constructs an empty
WeakMap . |
WeakMap(java.util.Map m)
Constructs a new
WeakMap with the same mappings as the
specified Map . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map.
|
boolean |
containsKey(java.lang.Object key)
Returns
true if this map contains a mapping for the
specified key. |
boolean |
containsValue(java.lang.Object value)
Returns
true if this map maps one or more keys to the
specified value. |
java.util.Set |
entrySet()
Returns a collection view of the mappings contained in this map.
|
boolean |
equals(java.lang.Object o) |
java.lang.Object |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this map, or
null if the map contains no mapping for this key. |
static java.lang.Object |
getValue(java.lang.Object ref)
Returns the referent of a
WeakReference , silently handling a
null argument. |
int |
hashCode() |
boolean |
isEmpty()
Returns
true if this map contains no key–value
mappings. |
java.util.Set |
keySet()
Returns a view of the keys contained in this map.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Associates the specified value with the specified key in this map.
|
void |
putAll(java.util.Map t)
Copies all of the mappings from the specified map to this map.
|
java.lang.Object |
remove(java.lang.Object key)
Removes the mapping for this key from this map if present.
|
int |
size()
Returns the number of key–value mappings in this map.
|
java.util.Collection |
values()
Returns a collection view of the values contained in this map.
|
public WeakMap()
WeakMap
.public WeakMap(java.util.Map m)
WeakMap
with the same mappings as the
specified Map
.m
- the map whose mappings are to be placed in this mappublic int size()
This is a non-modifying operation.
size
in interface java.util.Map
public boolean isEmpty()
true
if this map contains no key–value
mappings.
This is a non-modifying operation.
isEmpty
in interface java.util.Map
true
if this map contains no key–value
mappingspublic boolean containsKey(java.lang.Object key)
true
if this map contains a mapping for the
specified key.
This is a non-modifying operation.
containsKey
in interface java.util.Map
key
- the key whose presence in this map is to be testedtrue
if this map contains a mapping for the
specified keypublic boolean containsValue(java.lang.Object value)
true
if this map maps one or more keys to the
specified value.
This is a non-modifying operation.
containsValue
in interface java.util.Map
value
- the value whose presence in this map is to be testedtrue
if this map maps one or more keys to the
specified valuepublic java.lang.Object get(java.lang.Object key)
null
if the map contains no mapping for this key.
This is a non-modifying operation.
get
in interface java.util.Map
key
- the key whose associated value is to be returnednull
if the map contains no mapping for this keypublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
This is a modifying operation.
put
in interface java.util.Map
key
- the key with witch the specified value is to be associatedvalue
- the value to be associated with the specified key. This
must be a plain object, which is then wrapped in a
WeakReference
.null
if there was no mapping for the keypublic java.lang.Object remove(java.lang.Object key)
This is a modifying operation.
remove
in interface java.util.Map
key
- the key whose mapping is to be removed from the mapnull
if there was no mapping for the keypublic void putAll(java.util.Map t)
This is a modifying operation.
putAll
in interface java.util.Map
m
- mappings to be stored in this map. The values of those mappings
must be plain objects, which are then wrapped in instances of
WeakReference
.public void clear()
This is a modifying operation.
clear
in interface java.util.Map
public java.util.Set keySet()
This is a non-modifying operation.
keySet
in interface java.util.Map
public java.util.Collection values()
This is a non-modifying operation.
values
in interface java.util.Map
public java.util.Set entrySet()
This is a non-modifying operation.
entrySet
in interface java.util.Map
public boolean equals(java.lang.Object o)
equals
in interface java.util.Map
equals
in class java.lang.Object
public int hashCode()
hashCode
in interface java.util.Map
hashCode
in class java.lang.Object
public static java.lang.Object getValue(java.lang.Object ref)
WeakReference
, silently handling a
null
argument.
This static method is useful to wrap around the return values of
methods like get
.
ref
- must be either an instance of WeakReference
or
null
WeakReference
, or
null
if ref
is null