Map---WeakHashMap

发布时间 2023-11-24 19:59:57作者: anpeiyong

概述

Hash table based implementation of the <tt>Map</tt> interface, with <em>weak keys</em>.
An entry in a <tt>WeakHashMap</tt> 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 from other <tt>Map</tt> implementations.

基于Map的哈希表实现,有weak keys;

当WeakHashMap的key不再被使用时,entry将会被remove;

 

Both null values and the null key are supported.
This class has performance characteristics similar to those of the <tt>HashMap</tt> class, and has the same efficiency parameters of <em>initial capacity</em> and <em>load factor.

支持null key、null value

与HashMap有类似的性能;

 

Like most collection classes, this class is not synchronized.
A synchronized <tt>WeakHashMap</tt> may be constructed using the {@link Collections#synchronizedMap Collections.synchronizedMap} method.

线程非同步

可以使用Collections.synchronizedMap;

 

This class is intended primarily for use with key objects whose <tt>equals</tt> methods test for object identity using the <tt>==</tt> 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 <tt>WeakHashMap</tt> at some later time and be surprised that its entry has been removed.
This class will work perfectly well with key objects whose <tt>equals</tt> methods are not based upon object identity, such as <tt>String</tt> instances.
With such recreatable key objects, however, the automatic removal of <tt>WeakHashMap</tt> entries whose keys have been discarded may prove to be confusing.

 

The behavior of the <tt>WeakHashMap</tt> class depends in part upon the actions of the garbage collector, so several familiar (though not required) <tt>Map</tt> invariants do not hold for this class.
Because the garbage collector may discard keys at any time, a <tt>WeakHashMap</tt> may behave as though an unknown thread is silently removing entries.
In particular, even if you synchronize on a <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it is possible for the <tt>size</tt> method to return smaller values over time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and then <tt>true</tt>, for the <tt>containsKey</tt> method to return <tt>true</tt> and later <tt>false</tt> for a given key, for the <tt>get</tt> method to return a value for a given key but later return <tt>null</tt>, for the <tt>put</tt> method to return <tt>null</tt> and the <tt>remove</tt> method to return <tt>false</tt> for a key that previously appeared to be in the map, and for successive examinations of the key set, the value collection, and the entry set to yield successively smaller numbers of elements.

 

Each key object in a <tt>WeakHashMap</tt> 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.

WeakHashMap的每个key间接存储为弱引用

 

Implementation note:
The value objects in a <tt>WeakHashMap</tt> 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 <tt>WeakHashMap</tt> 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.
If the values in the map do not rely on the map holding strong references to them, one way to deal with this is to wrap values themselves within <tt>WeakReferences</tt> before inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>, and then unwrapping upon each <tt>get</tt>.

WeakHashMap的value是强引用

可以使用WeakReferences将value进行包装(m.put(key, new WeakReference(value)));

 

The iterators returned by the <tt>iterator</tt> method of the collections returned by all of this class's "collection view methods" are <i>fail-fast</i>: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own <tt>remove</tt> method, the iterator will throw a {@link ConcurrentModificationException}.
Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

iterator是fail-fast