public class HashSet extends AbstractSet implements Set, java.lang.Cloneable, java.io.Serializable, Rollbackable
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{
Collection
,
Set
,
TreeSet
,
Collections.synchronizedSet(Set)
,
HashMap
,
LinkedHashSet
,
Serialized Form$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
HashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75).
|
HashSet(Collection c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course).
|
HashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
|
HashSet(int initialCapacity,
float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
|
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
boolean |
add(java.lang.Object 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 |
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).
|
equals, hashCode, removeAll
$GET$CHECKPOINT, $SET$CHECKPOINT, addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
$GET$CHECKPOINT, $SET$CHECKPOINT, addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
public HashSet()
public HashSet(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMapjava.lang.IllegalArgumentException
- if the capacity is negativepublic HashSet(int initialCapacity, float 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 HashSet(Collection c)
c
- a collection of initial set elementsjava.lang.NullPointerException
- if c is nullpublic boolean add(java.lang.Object o)
add
in interface Collection
add
in interface Set
add
in class AbstractCollection
o
- the Object to add to this Setpublic void clear()
clear
in interface Collection
clear
in interface Set
clear
in class AbstractCollection
Iterator.remove()
public java.lang.Object clone()
clone
in class java.lang.Object
public boolean contains(java.lang.Object o)
contains
in interface Collection
contains
in interface Set
contains
in class AbstractCollection
o
- the Object to look forpublic boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface Set
isEmpty
in class AbstractCollection
size() == 0
.AbstractCollection.size()
public java.util.Iterator iterator()
iterator
in interface Collection
iterator
in interface Set
iterator
in class AbstractCollection
ConcurrentModificationException
public boolean remove(java.lang.Object o)
remove
in interface Collection
remove
in interface Set
remove
in class AbstractCollection
o
- the object to removeIterator.remove()
public int size()
size
in interface Collection
size
in interface Set
size
in class AbstractCollection
public void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Collection
$COMMIT
in interface Set
$COMMIT
in class AbstractSet
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Collection
$RESTORE
in interface Set
$RESTORE
in class AbstractSet
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)