java.util
public class LinkedHashSet<T> extends HashSet<T> implements Set<T>, Cloneable, Serializable
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
{@link ConcurrentModificationException} rather than exhibit
non-deterministic behavior.
Since: 1.4
See Also: hashCode Collection Set HashSet TreeSet synchronizedSet
UNKNOWN: updated to 1.4
Constructor Summary | |
---|---|
LinkedHashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75). | |
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.
| |
LinkedHashSet(Collection<? extends T> c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course). |
Parameters: initialCapacity the initial capacity of the backing HashMap
Throws: IllegalArgumentException if the capacity is negative
Parameters: initialCapacity the initial capacity of the backing HashMap loadFactor the load factor of the backing HashMap
Throws: IllegalArgumentException if either argument is negative, or if loadFactor is POSITIVE_INFINITY or NaN
Parameters: c a collection of initial set elements
Throws: NullPointerException if c is null