public class HashSet4D<T>
extends java.util.AbstractSet<T>
implements java.util.Set<T>, java.lang.Cloneable
Most operations are O(1), assuming no hash collisions. In the worst case (where all hashes collide), operations are O(n). Setting the initial capacity too low will force many resizing operations, but setting the initial capacity too high (or loadfactor too low) leads to wasted memory and slower iteration.
HashSet accepts the null key and null values. It is not synchronized,
so if you need multi-threaded access, consider using:
Set s = Collections.synchronizedSet(new HashSet(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Collection
,
Set
,
TreeSet
,
Collections.synchronizedSet(Set)
,
HashMap
,
LinkedHashSet
Constructor and Description |
---|
HashSet4D()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75).
|
HashSet4D(java.util.Collection<? extends T> c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course).
|
HashSet4D(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
|
HashSet4D(int initialCapacity,
double loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T o)
Adds the given Object to the set if it is not already in the Set.
|
void |
clear()
Empties this Set of all elements; this takes constant time.
|
java.lang.Object |
clone()
Returns a shallow copy of this Set.
|
boolean |
contains(java.lang.Object o)
Returns true if the supplied element is in this Set.
|
boolean |
isEmpty()
Returns true if this set has no elements in it.
|
java.util.Iterator<T> |
iterator()
Returns an Iterator over the elements of this Set, which visits the
elements in no particular order.
|
boolean |
remove(java.lang.Object o)
Removes the supplied Object from this Set if it is in the Set.
|
int |
size()
Returns the number of elements in this Set (its cardinality).
|
addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
public HashSet4D()
public HashSet4D(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMapjava.lang.IllegalArgumentException
- if the capacity is negativepublic HashSet4D(int initialCapacity, double loadFactor)
initialCapacity
- the initial capacity of the backing HashMaploadFactor
- the load factor of the backing HashMapjava.lang.IllegalArgumentException
- if either argument is negative, or
if loadFactor is POSITIVE_INFINITY or NaNpublic HashSet4D(java.util.Collection<? extends T> c)
c
- a collection of initial set elementsjava.lang.NullPointerException
- if c is nullpublic boolean add(T o)
public void clear()
public java.lang.Object clone()
clone
in class java.lang.Object
public boolean contains(java.lang.Object o)
public boolean isEmpty()
public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
iterator
in interface java.util.Collection<T>
iterator
in interface java.util.Set<T>
iterator
in class java.util.AbstractCollection<T>
ConcurrentModificationException
public boolean remove(java.lang.Object o)