public class LinkedHashSet extends HashSet implements Set, java.lang.Cloneable, java.io.Serializable, Rollbackable
It uses a hash-bucket approach; that is, hash collisions are handled
by linking the new node off of the pre-existing node (or list of
nodes). In this manner, techniques such as linear probing (which
can cause primary clustering) and rehashing (which does not fit very
well with Java's method of precomputing hash codes) are avoided. In
addition, this maintains a doubly-linked list which tracks insertion
order. Note that the insertion order is not modified if an
add
simply reinserts an element in the set.
One of the nice features of tracking insertion order is that you can
copy a set, and regardless of the implementation of the original,
produce the same results when iterating over the copy. This is possible
without needing the overhead of TreeSet
.
Under ideal circumstances (no collisions), LinkedHashSet offers O(1) performance on most operations. In the worst case (all elements map to the same hash code -- very unlikely), most operations are O(n).
LinkedHashSet accepts the null entry. It is not synchronized, so if
you need multi-threaded access, consider using:
Set s = Collections.synchronizedSet(new LinkedHashSet(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a{
Object.hashCode()
,
Collection
,
Set
,
HashSet
,
TreeSet
,
Collections.synchronizedSet(Set)
,
Serialized Form$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
LinkedHashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75).
|
LinkedHashSet(Collection c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course).
|
LinkedHashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
|
LinkedHashSet(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.
|
add, clear, clone, contains, isEmpty, iterator, remove, size
equals, hashCode, removeAll
$GET$CHECKPOINT, $SET$CHECKPOINT, addAll, containsAll, retainAll, toArray, toArray, toString
public LinkedHashSet()
public LinkedHashSet(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMapjava.lang.IllegalArgumentException
- if the capacity is negativepublic LinkedHashSet(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 LinkedHashSet(Collection c)
c
- a collection of initial set elementsjava.lang.NullPointerException
- if c is nullpublic void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Collection
$COMMIT
in interface Set
$COMMIT
in class HashSet
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 HashSet
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)