Class WeakKey


  • public class WeakKey
    extends WeakReference<Object>
    Implement a WeakReference key to be used in a collection. Being a WeakReference, it will not prevent the key from being Garbage Collected. The key can only be created with a reference queue so that users of this class provide cleanup logic which uses the items in the reference queue to cleanup entries in the collection. Note that the ReferenceQueue will contain the WeakKey instance that assocaited with the referent that was GC'd. So, the elements on the ReferenceQueue can be used to directly access and remove entries in the collection it is a key for. For example, one could do something like the following in cleanup logic: Object gcKey = null; while ((gcKey = q.poll()) != null) { WeakKey wk = (WeakKey) gcKey; removedEntry = collection.remove(wk); ... }
    • Method Detail

      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • comparisonKey

        public static WeakKey comparisonKey​(Object checkKey)
        Return an instance of WeakKey that can be used in comparsion operations. For example, it can be used to lookup a key in a collection that has a WeakKey as the key.
        Parameters:
        checkKey - the key value
        Returns:
        an instance of WeakKey for the value