Class WeakHashSet<E>

  • Type Parameters:
    E - The type of element stored in the set.
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

    public class WeakHashSet<E>
    extends java.util.AbstractSet<E>
    implements java.util.Set<E>
    A class that implements the Set interface, backed by a WeakHashMap. This means that members of the set will be collected by the garbage collector when they are no longer in ordinary use.

    This class was created referencing HashSet 1.25 01/12/03 by Doug Lea, Josh Bloch, and Mark Reinhold.

    Author:
    Garret Wilson
    See Also:
    HashSet, WeakHashMap
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakHashSet()
      Constructs a new, empty set.
      WeakHashSet​(int initialCapacity)
      Constructs a new, empty set.
      WeakHashSet​(int initialCapacity, float loadFactor)
      Constructs a new, empty set.
      WeakHashSet​(java.util.Collection<E> collection)
      Constructs a new set containing the elements in the specified collection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E o)
      Adds the specified element to this set if it is not already present (optional operation).
      void clear()
      Removes all of the elements from this set (optional operation).
      boolean contains​(java.lang.Object o)
      Returns true if this set contains the specified element.
      boolean isEmpty()
      Returns true if this set contains no elements.
      java.util.Iterator<E> iterator()
      Returns an iterator over the elements in this set.
      boolean remove​(java.lang.Object o)
      Removes the specified element from this set if it is present (optional operation).
      int size()
      Returns the number of elements in this set (its cardinality).
      • Methods inherited from class java.util.AbstractSet

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

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

        clone, 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, equals, hashCode, removeAll, retainAll, spliterator, toArray, toArray
    • Constructor Detail

      • WeakHashSet

        public WeakHashSet()
        Constructs a new, empty set. The backing WeakHashMap instance has default initial capacity (16) and load factor (0.75).
      • WeakHashSet

        public WeakHashSet​(java.util.Collection<E> collection)
        Constructs a new set containing the elements in the specified collection. The WeakHashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.
        Parameters:
        collection - The collection whose elements are to be placed into this set.
        Throws:
        java.lang.NullPointerException - Thrown if the specified collection is null.
      • WeakHashSet

        public WeakHashSet​(int initialCapacity,
                           float loadFactor)
        Constructs a new, empty set. The backing WeakHashMap instance has the specified initial capacity and the specified load factor.
        Parameters:
        initialCapacity - The initial capacity of the hash map.
        loadFactor - The load factor of the hash map.
        Throws:
        java.lang.IllegalArgumentException - Thrown if the initial capacity is less than zero, or if the load factor is nonpositive.
      • WeakHashSet

        public WeakHashSet​(int initialCapacity)
        Constructs a new, empty set. The backing WeakHashMap instance has the specified initial capacity and default load factor, which is 0.75.
        Parameters:
        initialCapacity - The initial capacity of the hash table.
        Throws:
        java.lang.IllegalArgumentException - Thrown if the initial capacity is less than zero.
    • Method Detail

      • size

        public int size()
        Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
        Returns:
        the number of elements in this set (its cardinality).
      • isEmpty

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

        public boolean contains​(java.lang.Object o)
        Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.Set<E>
        Overrides:
        contains in class java.util.AbstractCollection<E>
        Parameters:
        o - element whose presence in this set is to be tested.
        Returns:
        true if this set contains the specified element.
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this set (optional).
        java.lang.NullPointerException - if the specified element is null and this set does not support null elements (optional).
      • iterator

        public java.util.Iterator<E> iterator()
        Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
        Returns:
        an iterator over the elements in this set.
      • add

        public boolean add​(E o)
        Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e)). If this set already contains the specified element, the call leaves this set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

        The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throwing an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the the elements that they may contain.

        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.Set<E>
        Overrides:
        add in class java.util.AbstractCollection<E>
        Parameters:
        o - element to be added to this set.
        Returns:
        true if this set did not already contain the specified element.
        Throws:
        java.lang.UnsupportedOperationException - if the add method is not supported by this set.
        java.lang.ClassCastException - if the class of the specified element prevents it from being added to this set.
        java.lang.NullPointerException - if the specified element is null and this set does not support null elements.
        java.lang.IllegalArgumentException - if some aspect of the specified element prevents it from being added to this set.
      • remove

        public boolean remove​(java.lang.Object o)
        Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the set contains such an element. Returns true if the set contained the specified element (or equivalently, if the set changed as a result of the call). (The set will not contain the specified element once the call returns.)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.Set<E>
        Overrides:
        remove in class java.util.AbstractCollection<E>
        Parameters:
        o - object to be removed from this set, if present.
        Returns:
        true if the set contained the specified element.
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this set (optional).
        java.lang.NullPointerException - if the specified element is null and this set does not support null elements (optional).
        java.lang.UnsupportedOperationException - if the remove method is not supported by this set.
      • clear

        public void clear()
        Removes all of the elements from this set (optional operation). This set will be empty after this call returns (unless it throws an exception).
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.Set<E>
        Overrides:
        clear in class java.util.AbstractCollection<E>
        Throws:
        java.lang.UnsupportedOperationException - if the clear method is not supported by this set.