Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class LinkedHashSet

java.lang.Object
|
+--java.util.AbstractCollection
   |
   +--java.util.AbstractSet
      |
      +--java.util.HashSet
         |
         +--java.util.LinkedHashSet

All Implemented Interfaces:

Set, Cloneable, Serializable, Set, Cloneable, Serializable, Set, Collection


public class LinkedHashSet

extends HashSet

implements Set, Cloneable, Serializable

This class provides a hashtable-backed implementation of the Set interface, with predictable traversal order.

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.

Since:Author:See Also:

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(java.util.Collection c)

Construct a new HashSet with the same elements as are in the supplied collection (eliminating any duplicates, of course).

Constructor Details

LinkedHashSet

public LinkedHashSet()

Construct a new, empty HashSet whose backing HashMap has the default capacity (11) and loadFacor (0.75).


LinkedHashSet

public LinkedHashSet(int initialCapacity)

Construct a new, empty HashSet whose backing HashMap has the supplied capacity and the default load factor (0.75).

Parameters:

Throws:


LinkedHashSet

public LinkedHashSet(int initialCapacity, float loadFactor)

Construct a new, empty HashSet whose backing HashMap has the supplied capacity and load factor.

Parameters:

Throws:


LinkedHashSet

public LinkedHashSet(java.util.Collection c)

Construct a new HashSet with the same elements as are in the supplied collection (eliminating any duplicates, of course). The backing storage has twice the size of the collection, or the default size of 11, whichever is greater; and the default load factor (0.75).

Parameters:

Throws: