Package com.unboundid.util
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:
java.lang.Iterable<T>,java.util.Collection<T>,java.util.Set<T>
@Mutable @ThreadSafety(level=NOT_THREADSAFE) public final class WeakHashSet<T> extends java.lang.Object implements java.util.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 WeakHashSet()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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T e)Adds the provided element to this set, if it does not already exist.booleanaddAll(java.util.Collection<? extends T> c)Adds any elements from the provided collection to this set if they were not already present.TaddAndGet(T e)Adds the provided element to the set if it does not already exist, and retrieves the value stored in the set.voidclear()Clears the contents of this set.booleancontains(java.lang.Object e)Indicates whether this set contains the specified element.booleancontainsAll(java.util.Collection<?> c)Indicates whether this set currently contains all of the elements in the provided collection.booleanequals(java.lang.Object o)Indicates whether the provided object is equal to this set.Tget(T e)Retrieves the existing instance of the provided element from this set.inthashCode()Retrieves a hash code for this set.booleanisEmpty()Indicates whether this set is currently empty.java.util.Iterator<T>iterator()Retrieves an iterator across all elements in this set.booleanremove(java.lang.Object e)Removes the specified element from this set, if it exists.booleanremoveAll(java.util.Collection<?> c)Removes all of the elements of the provided collection from this set.booleanretainAll(java.util.Collection<?> c)Removes all elements from this set which are not contained in the provided collection.intsize()Retrieves the number of elements currently held in this set.java.lang.Object[]toArray()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.java.lang.StringtoString()Retrieves a string representation of this set.
-
-
-
Constructor Detail
-
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 benull.
-
-
Method Detail
-
clear
public void clear()
Clears the contents of this set.
-
isEmpty
public boolean isEmpty()
Indicates whether this set is currently empty.
-
size
public int size()
Retrieves the number of elements currently held in this set.
-
contains
public boolean contains(java.lang.Object e)
Indicates whether this set contains the specified element.
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
Indicates whether this set currently contains all of the elements in the provided collection.
-
get
public T get(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
nullif the provided element is not contained in this set.
-
add
public boolean add(T e)
Adds the provided element to this set, if it does not already exist.- Specified by:
addin interfacejava.util.Collection<T>- Specified by:
addin interfacejava.util.Set<T>- Parameters:
e- The element to be added to the set if it does not already exist.- Returns:
trueif the element was added to the set (because it was not already present), orfalseif the element was not added (because it was already in the set).
-
addAll
public boolean addAll(java.util.Collection<? extends T> c)
Adds any elements from the provided collection to this set if they were not already present.- Specified by:
addAllin interfacejava.util.Collection<T>- Specified by:
addAllin interfacejava.util.Set<T>- Parameters:
c- The collection containing elements to add.- Returns:
trueif at least one of the elements was not already in the set and was added, orfalseif no elements were added because they were already all present.
-
addAndGet
public T addAndGet(T e)
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(java.lang.Object e)
Removes the specified element from this set, if it exists.
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
Removes all of the elements of the provided collection from this set.- Specified by:
removeAllin interfacejava.util.Collection<T>- Specified by:
removeAllin interfacejava.util.Set<T>- Parameters:
c- The collection containing the elements to remove from this set.- Returns:
trueif at least one of the elements from the provided collection were contained in and therefore removed from the set, orfalseif none of the elements in the given collection were contained in this set.
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
Removes all elements from this set which are not contained in the provided collection.- Specified by:
retainAllin interfacejava.util.Collection<T>- Specified by:
retainAllin interfacejava.util.Set<T>- Parameters:
c- The collection of elements to be retained.- Returns:
trueif this set contained at least one element not in the provided collection that was therefore removed, orfalseif this set did not have any elements that were not in the provided collection.
-
iterator
public java.util.Iterator<T> iterator()
Retrieves an iterator across all elements in this set.
-
toArray
public java.lang.Object[] toArray()
Retrieves an array containing all of the elements currently held in this set.
-
toArray
public <E> E[] toArray(E[] a)
Retrieves an array containing all of the elements currently held in this set.- Specified by:
toArrayin interfacejava.util.Collection<T>- Specified by:
toArrayin interfacejava.util.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
nullelement 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.
-
equals
public boolean equals(java.lang.Object o)
Indicates whether the provided object is equal to this set.- Specified by:
equalsin interfacejava.util.Collection<T>- Specified by:
equalsin interfacejava.util.Set<T>- Overrides:
equalsin classjava.lang.Object- Parameters:
o- The object for which to make the determination.- Returns:
trueif the provided object is a non-nullset with the same elements as this set, orfalseif not.
-
toString
public java.lang.String toString()
Retrieves a string representation of this set.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of this set.
-
-