The java.lang.ref package provides classes that model reference types in Java, such as Reference, SoftReferece, WeakReference, and PhantomReference.
Still do not know these references in Java? See this question: Canonicalized Mapping and WeakReference
One of the Java SE classes that uses WeakReference is the java.util.WeakHashMap
, where we have key
as a weak reference, ie even with reference it is collected in the next Garbase cycle Collector (if there is no other StrongReference).
"... WeakHashMap may behave as though an unknown thread is silently removing entries. "
Javadocs Source: link
Interestingly, we do not have a Set implementation with this semantics - WeakSet, at least not on the Java SE platform.
How can we get an instance of WeakSet?