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:
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 theSet
interface, backed by aWeakHashMap
. 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)
Returnstrue
if this set contains the specified element.boolean
isEmpty()
Returnstrue
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.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
-
-
-
-
Constructor Detail
-
WeakHashSet
public WeakHashSet()
Constructs a new, empty set. The backingWeakHashMap
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. 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:
java.lang.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:
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 backingWeakHashMap
instance has the specified initial capacity and default load factor, which is0.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 thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
-
isEmpty
public boolean isEmpty()
Returnstrue
if this set contains no elements.
-
contains
public boolean contains(java.lang.Object o)
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 interfacejava.util.Collection<E>
- Specified by:
contains
in interfacejava.util.Set<E>
- Overrides:
contains
in classjava.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).
-
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 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 interfacejava.util.Collection<E>
- Specified by:
add
in interfacejava.util.Set<E>
- Overrides:
add
in classjava.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 theadd
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 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 interfacejava.util.Collection<E>
- Specified by:
remove
in interfacejava.util.Set<E>
- Overrides:
remove
in classjava.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 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).
-
-