public class LinkedHashSet4D<T> extends HashSet4D<T> implements java.util.Set<T>, java.lang.Cloneable
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
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Object.hashCode()
,
Collection
,
Set
,
HashSet
,
TreeSet
,
Collections.synchronizedSet(Set)
Constructor and Description |
---|
LinkedHashSet4D()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFactor (0.75).
|
LinkedHashSet4D(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).
|
LinkedHashSet4D(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
|
LinkedHashSet4D(int initialCapacity,
double loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
|
add, clear, clone, contains, isEmpty, iterator, remove, size
addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
public LinkedHashSet4D()
public LinkedHashSet4D(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMapjava.lang.IllegalArgumentException
- if the capacity is negativepublic LinkedHashSet4D(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 LinkedHashSet4D(java.util.Collection<? extends T> c)
c
- a collection of initial set elementsjava.lang.NullPointerException
- if c is null