Package com.globalmentor.collections
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>
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 Summary
ConstructorsConstructorDescriptionConstructs a new, empty set.WeakHashSet
(int initialCapacity) Constructs a new, empty set.WeakHashSet
(int initialCapacity, float loadFactor) Constructs a new, empty set.WeakHashSet
(Collection<E> collection) Constructs a new set containing the elements in the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
Returnstrue
if this set contains the specified element.boolean
isEmpty()
Returnstrue
if this set contains no elements.iterator()
Returns an iterator over the elements in this set.boolean
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
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
WeakHashSet
public WeakHashSet()Constructs a new, empty set. The backingWeakHashMap
instance has default initial capacity (16) and load factor (0.75). -
WeakHashSet
Constructs a new set containing the elements in the specified collection. TheWeakHashMap
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 isnull
.
-
WeakHashSet
public WeakHashSet(int initialCapacity, float loadFactor) Constructs a new, empty set. The backingWeakHashMap
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 backingWeakHashMap
instance has the specified initial capacity and default load factor, which is0.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 thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this set (its cardinality).
-
isEmpty
public boolean isEmpty()Returnstrue
if this set contains no elements.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
true
if this set contains no elements.
-
contains
Returnstrue
if this set contains the specified element. More formally, returnstrue
if and only if this set contains an elemente
such that(o==null ? e==null : o.equals(e))
.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<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
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). -
add
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 elemente
such that(o==null ? e==null : o.equals(e))
. If this set already contains the specified element, the call leaves this set unchanged and returnsfalse
. 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 forCollection.add
. Individual set implementations should clearly document any restrictions on the the elements that they may contain.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<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 theadd
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
Removes the specified element from this set if it is present (optional operation). More formally, removes an elemente
such that(o==null ? e==null : o.equals(e))
, if the set contains such an element. Returnstrue
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 interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<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 theremove
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 interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
- if theclear
method is not supported by this set.
-