Class WeakSet<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

    public class WeakSet<T>
    extends java.util.AbstractSet<T>
    implements java.io.Serializable, java.lang.Cloneable
    A weak set for entries that will be automatically removed when no references to them are existing any more.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.ref.ReferenceQueue queue
      The reference queue used to get object removal notifications.
      protected java.util.Set serialized_set  
      protected java.util.Set set
      The set which will be used for element storage.
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakSet()
      Construct a WeakSet based on a HashSet.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.lang.Object obj)
      Add an element to the set.
      void clear()
      Removes all of the elements from this set.
      java.lang.Object clone()
      Returns a shallow copy of this WeakSet instance: the elements themselves are not cloned.
      boolean contains​(java.lang.Object obj)
      Returns true if this set contains the specified element.
      protected void expungeStaleEntries()
      Remove garbage collected entries.
      boolean isEmpty()
      Returns true if this set contains no elements.
      java.util.Iterator iterator()
      Return an iteration over the elements in the set.
      protected java.lang.Object readResolve()
      Perform special handling on serialization.
      boolean remove​(java.lang.Object obj)
      Removes the given element from this set if it is present.
      int size()
      Return the size of the set.
      java.lang.Object[] toArray()
      Convert the set to an array.
      java.lang.Object[] toArray​(java.lang.Object[] result)
      Convert the set to an array.
      protected java.lang.Object writeReplace()
      Perform special handling on serialization.
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode, removeAll
      • Methods inherited from class java.util.AbstractCollection

        addAll, containsAll, retainAll, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        addAll, containsAll, retainAll, spliterator
    • Field Detail

      • set

        protected transient java.util.Set set
        The set which will be used for element storage.
      • queue

        protected transient java.lang.ref.ReferenceQueue queue
        The reference queue used to get object removal notifications.
      • serialized_set

        protected java.util.Set serialized_set
    • Constructor Detail

      • WeakSet

        public WeakSet()
        Construct a WeakSet based on a HashSet.
    • Method Detail

      • size

        public int size()
        Return the size of the set.
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.Set<T>
        Specified by:
        size in class java.util.AbstractCollection<T>
        Returns:
        The size of the set.
      • iterator

        public java.util.Iterator iterator()
        Return an iteration over the elements in the set.
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.Set<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
        Returns:
        An iteration over the elements in the set.
      • toArray

        public java.lang.Object[] toArray()
        Convert the set to an array.
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class java.util.AbstractCollection<T>
      • toArray

        public java.lang.Object[] toArray​(java.lang.Object[] result)
        Convert the set to an array.
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class java.util.AbstractCollection<T>
      • add

        public boolean add​(java.lang.Object obj)
        Add an element to the set.
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.Set<T>
        Overrides:
        add in class java.util.AbstractCollection<T>
        Parameters:
        obj - Element to add to the set.
        Returns:
        True if the element was added.
      • isEmpty

        public boolean isEmpty()
        Returns true if this set contains no elements.
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.Set<T>
        Overrides:
        isEmpty in class java.util.AbstractCollection<T>
        Returns:
        true if this set contains no elements.
      • contains

        public boolean contains​(java.lang.Object obj)
        Returns true if this set contains the specified element.
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.Set<T>
        Overrides:
        contains in class java.util.AbstractCollection<T>
        Parameters:
        obj - Element whose presence in this set is to be tested.
        Returns:
        true if this set contains the specified element.
      • remove

        public boolean remove​(java.lang.Object obj)
        Removes the given element from this set if it is present.
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.Set<T>
        Overrides:
        remove in class java.util.AbstractCollection<T>
        Parameters:
        obj - Object to be removed from this set, if present.
        Returns:
        true if the set contained the specified element.
      • clear

        public void clear()
        Removes all of the elements from this set.
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.Set<T>
        Overrides:
        clear in class java.util.AbstractCollection<T>
      • clone

        public java.lang.Object clone()
        Returns a shallow copy of this WeakSet instance: the elements themselves are not cloned.
        Overrides:
        clone in class java.lang.Object
        Returns:
        A shallow copy of this set.
      • expungeStaleEntries

        protected final void expungeStaleEntries()
        Remove garbage collected entries.
      • writeReplace

        protected java.lang.Object writeReplace()
                                         throws java.io.ObjectStreamException
        Perform special handling on serialization.
        Throws:
        java.io.ObjectStreamException
      • readResolve

        protected java.lang.Object readResolve()
                                        throws java.io.ObjectStreamException
        Perform special handling on serialization.
        Throws:
        java.io.ObjectStreamException