Class WeakHashSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
com.globalmentor.collections.WeakHashSet<E>
Type Parameters:
E - The type of element stored in the set.
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

public class WeakHashSet<E> extends AbstractSet<E> implements 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:
  • Constructor Details

    • 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(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:
      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:
      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:
      IllegalArgumentException - Thrown if the initial capacity is less than zero.
  • Method Details

    • 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 Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class 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 Collection<E>
      Specified by:
      isEmpty in interface Set<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:
      true if this set contains no elements.
    • contains

      public boolean contains(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 Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      o - element whose presence in this set is to be tested.
      Returns:
      true if this set contains the specified element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this set (optional).
      NullPointerException - if the specified element is null and this set does not support null elements (optional).
    • iterator

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

      public boolean remove(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 Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      o - object to be removed from this set, if present.
      Returns:
      true if the set contained the specified element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this set (optional).
      NullPointerException - if the specified element is null and this set does not support null elements (optional).
      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 Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
      Throws:
      UnsupportedOperationException - if the clear method is not supported by this set.