Class WeakHashSet<T>

java.lang.Object
com.unboundid.util.WeakHashSet<T>
Type Parameters:
T - The type of element held in this set.
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>

@Mutable @ThreadSafety(level=NOT_THREADSAFE) public final class WeakHashSet<T> extends Object implements Set<T>
This class provides a weak hash set, which maintains weak references to the elements it contains, so that they will be removed automatically once there are no more normal references to them.

Note that because this set uses weak references, elements may disappear from the set at any time without being explicitly removed. This means that care must be taken to ensure that the result of one method must not be considered authoritative for subsequent calls to the same method or other methods in this class.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new weak hash set with the default initial capacity.
    WeakHashSet(int initialCapacity)
    Creates a new weak hash set with the specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(T e)
    Adds the provided element to this set, if it does not already exist.
    boolean
    addAll(Collection<? extends T> c)
    Adds any elements from the provided collection to this set if they were not already present.
    Adds the provided element to the set if it does not already exist, and retrieves the value stored in the set.
    void
    Clears the contents of this set.
    boolean
    Indicates whether this set contains the specified element.
    boolean
    Indicates whether this set currently contains all of the elements in the provided collection.
    boolean
    Indicates whether the provided object is equal to this set.
    get(T e)
    Retrieves the existing instance of the provided element from this set.
    int
    Retrieves a hash code for this set.
    boolean
    Indicates whether this set is currently empty.
    Retrieves an iterator across all elements in this set.
    boolean
    Removes the specified element from this set, if it exists.
    boolean
    Removes all of the elements of the provided collection from this set.
    boolean
    Removes all elements from this set which are not contained in the provided collection.
    int
    Retrieves the number of elements currently held in this set.
    Retrieves an array containing all of the elements currently held in this set.
    <E> E[]
    toArray(E[] a)
    Retrieves an array containing all of the elements currently held in this set.
    Retrieves a string representation of this set.

    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

    spliterator
  • Constructor Details

    • WeakHashSet

      public WeakHashSet()
      Creates a new weak hash set with the default initial capacity.
    • WeakHashSet

      public WeakHashSet(int initialCapacity)
      Creates a new weak hash set with the specified initial capacity.
      Parameters:
      initialCapacity - The initial capacity for this weak hash set. It must not be null.
  • Method Details

    • clear

      public void clear()
      Clears the contents of this set.
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface Set<T>
    • isEmpty

      public boolean isEmpty()
      Indicates whether this set is currently empty.
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface Set<T>
      Returns:
      true if this set is empty, or false if not.
    • size

      public int size()
      Retrieves the number of elements currently held in this set.
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface Set<T>
      Returns:
      The number of elements currently held in this set.
    • contains

      public boolean contains(@NotNull Object e)
      Indicates whether this set contains the specified element.
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface Set<T>
      Parameters:
      e - The element for which to make the determination.
      Returns:
      true if this set contains the specified element, or false if not.
    • containsAll

      public boolean containsAll(@NotNull Collection<?> c)
      Indicates whether this set currently contains all of the elements in the provided collection.
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface Set<T>
      Parameters:
      c - The collection for which to make the determination.
      Returns:
      true if this set currently contains all of the elements in the provided collection, or false if not.
    • get

      @Nullable public T get(@NotNull T e)
      Retrieves the existing instance of the provided element from this set.
      Parameters:
      e - The object for which to obtain the existing element.
      Returns:
      The existing instance of the provided element, or null if the provided element is not contained in this set.
    • add

      public boolean add(@NotNull T e)
      Adds the provided element to this set, if it does not already exist.
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Set<T>
      Parameters:
      e - The element to be added to the set if it does not already exist.
      Returns:
      true if the element was added to the set (because it was not already present), or false if the element was not added (because it was already in the set).
    • addAll

      public boolean addAll(@NotNull Collection<? extends T> c)
      Adds any elements from the provided collection to this set if they were not already present.
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface Set<T>
      Parameters:
      c - The collection containing elements to add.
      Returns:
      true if at least one of the elements was not already in the set and was added, or false if no elements were added because they were already all present.
    • addAndGet

      Adds the provided element to the set if it does not already exist, and retrieves the value stored in the set.
      Parameters:
      e - The element to be added to the set if it does not already exist.
      Returns:
      An existing version of the provided element if it was already in the set, or the provided object if it was just added.
    • remove

      public boolean remove(@NotNull Object e)
      Removes the specified element from this set, if it exists.
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface Set<T>
      Parameters:
      e - The element to be removed from this set.
      Returns:
      true if the element existed in the set and was removed, or false if not.
    • removeAll

      public boolean removeAll(@NotNull Collection<?> c)
      Removes all of the elements of the provided collection from this set.
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface Set<T>
      Parameters:
      c - The collection containing the elements to remove from this set.
      Returns:
      true if at least one of the elements from the provided collection were contained in and therefore removed from the set, or false if none of the elements in the given collection were contained in this set.
    • retainAll

      public boolean retainAll(@NotNull Collection<?> c)
      Removes all elements from this set which are not contained in the provided collection.
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface Set<T>
      Parameters:
      c - The collection of elements to be retained.
      Returns:
      true if this set contained at least one element not in the provided collection that was therefore removed, or false if this set did not have any elements that were not in the provided collection.
    • iterator

      Retrieves an iterator across all elements in this set.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Set<T>
      Returns:
      An iterator across all elements in this set.
    • toArray

      @NotNull public Object[] toArray()
      Retrieves an array containing all of the elements currently held in this set.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface Set<T>
      Returns:
      An array containing all of the elements currently held in this set.
    • toArray

      @NotNull public <E> E[] toArray(@NotNull E[] a)
      Retrieves an array containing all of the elements currently held in this set.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface Set<T>
      Type Parameters:
      E - The type of element for the given array.
      Parameters:
      a - An array into which the elements will be added if there is sufficient space.
      Returns:
      The provided array (with the first null element depicting the end of the set elements if the given array is larger than this set), or a newly-allocated array if the provided array was not large enough.
    • hashCode

      public int hashCode()
      Retrieves a hash code for this set.
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface Set<T>
      Overrides:
      hashCode in class Object
      Returns:
      A hash code for this set.
    • equals

      public boolean equals(@Nullable Object o)
      Indicates whether the provided object is equal to this set.
      Specified by:
      equals in interface Collection<T>
      Specified by:
      equals in interface Set<T>
      Overrides:
      equals in class Object
      Parameters:
      o - The object for which to make the determination.
      Returns:
      true if the provided object is a non-null set with the same elements as this set, or false if not.
    • toString

      Retrieves a string representation of this set.
      Overrides:
      toString in class Object
      Returns:
      A string representation of this set.