LinkedHashSet<T> Class
The LinkedHashSet<T> class implements the ISet<T> interface. Its instance has both the hash table and the
doubly-linked list to maintain the elements so that it has predictable
iteration order, like that of the LinkedHashSet
class in Java.
public class LinkedHashSet<T> : ISet<T>
where T : notnull
Type Parameters
- T
-
notnull
The type of elements maintained by this set.
- Inheritance
-
Linked
Hash Set<T>
- Implements
Remarks
The linked list keeps the iteration order corresponding to the insertion
order in which you insert elements into the set. Note that the
insertion order is not affected when you re-inserted any into it (i.e., the
Add(T) method returned false
).
The hash table uses Separate Chaining for collision resolution and has the capacity, size, and load factor. The capacity represents the number of entries in the hash table. In this implementation, it is a power of two. The size is the number of elements that the hash table contains. The set rehashes the hash table when the size divided by the capacity is close to the load factor.Rehashing makes the capacity of the hash table double.
To check whether two sets are equal, with iteration order taken into account, use the Enumerable.SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method as follows:
Note that the SetEquals(IEnumerable<T>) and SetEquals(LinkedHashSet<T>) methods ignore the iteration order and return set equality; the object.Equals(object) method returns reference equality.
The minimum and maximum capacities are DefaultInitialCapacity
(16
) and DefaultMaxCapacity (0x40000000
),
respectively.
As mentioned, if the number of elements in the set exceeds the product of
the capacity and the load factor, it rehashes its hash table with the
capacity updated to double. However, this implementation restricts the
maximum capacity to DefaultMaxCapacity (0x40000000
).
So, once the capacity reaches its maximum, rehashing will no longer occur.
Note that since the implementation uses Separate Chaining, it is possible
to add up to int.MaxValue (0x7fffffff
) elements to the
set even after the capacity reaches its maximumunless it throws an OutOfMemoryException.
If the number of elements in the set reaches the maximum value (int.MaxValue), any attempt to add elements throws an InsufficientMemoryException (e.g., with the Add(T) method).
Constructors
Linked |
Initializes a new instance of the LinkedHashSet<T> class. |
Linked |
Initializes a new instance of the LinkedHashSet<T> class. |
Fields
Default |
The default maximum capacity. |
Default |
The default maximum size. |
Default |
The default initial capacity. This is also the minimum capacity. |
Default |
The default load factor. |
Properties
Load |
Gets the load factor. |
Count | |
Is |
|
Capacity |
Gets the capacity and limit. |
First |
Gets the first and last nodes, or |
Methods
Add(T) | |
Clear() | |
Contains(T) | |
Copy |
|
Except |
|
Get |
|
Intersect |
|
Is |
|
Is |
|
Is |
|
Is |
|
Overlaps(IEnumerable<T>) | |
Remove(T) | |
Set |
|
Symmetric |
|
Union |
|
Intersect |
Modifies the current LinkedHashSet<T> object to contain only elements that are present in that object and in the specified LinkedHashSet<T>. |
Union |
Modifies the current LinkedHashSet<T> object to contain all elements that are present in itself, the specified set, or both. |
Symmetric |
Modifies the current LinkedHashSet<T> object to contain only elements that are present either in that object or in the specified set, but not both. |
Set |
Determines whether a LinkedHashSet<T> object and the specified set contain the same elements. |
Overlaps(Linked |
Determines whether the current LinkedHashSet<T> object and a specified set share common elements. |
Is |
Determines whether a LinkedHashSet<T> object is a subset of the specified set. |
Is |
Determines whether a LinkedHashSet<T> object is a superset of the specified set. |
Is |
Determines whether a LinkedHashSet<T> object is a proper subset of the specified set. |
Is |
Determines whether a LinkedHashSet<T> object is a proper superset of the specified set. |
Get |
Gets the backend array of the hash table. |
Equals(object) | (Inherited from object) |
GetHashCode() | (Inherited from object) |
GetType() | (Inherited from object) |
MemberwiseClone() | (Inherited from object) |
ToString() | (Inherited from object) |
Explicit Interface Implementations
Constructors Detail
LinkedHashSet(int, float)
Initializes a new instance of the LinkedHashSet<T> class.
public LinkedHashSet([int initialCapacity = 16], [float loadFactor = 0.75])
Parameters
- initialCapacity
- int
The initial capacity.
- loadFactor
- float
The load factor.
Remarks
The capacity is the smallest power-of-two number equal to or greater
than the specified initialCapacity
.
LinkedHashSet(HashTableConstants, int, float)
Initializes a new instance of the LinkedHashSet<T> class.
protected LinkedHashSet(HashTableConstants constants, int initialCapacity, float loadFactor)
Parameters
- constants
-
Hash
Table Constants
The constants for the hash table.
- initialCapacity
- int
The initial capacity.
- loadFactor
- float
The load factor.
Fields Detail
DefaultMaxCapacity
The default maximum capacity.
public const int DefaultMaxCapacity = 1073741824
Field Value
DefaultMaxSize
The default maximum size.
public const int DefaultMaxSize = 2147483647
Field Value
DefaultInitialCapacity
The default initial capacity. This is also the minimum capacity.
public const int DefaultInitialCapacity = 16
Field Value
DefaultLoadFactor
The default load factor.
public const float DefaultLoadFactor = 0.75
Field Value
Properties Detail
LoadFactor
Gets the load factor.
public float LoadFactor { get; }
Property Value
Count
public int Count { get; }
Property Value
Implements
IsReadOnly
public bool IsReadOnly { get; }
Property Value
Implements
CapacityAndLimit
Gets the capacity and limit.
protected (int Capacity, int Limit) CapacityAndLimit { get; }
Property Value
FirstAndLastNode
Gets the first and last nodes, or null
when this set is empty.
protected (LinkedHashSet<T>.ProtectedNode First, LinkedHashSet<T>.ProtectedNode Last) FirstAndLastNode { get; }
Property Value
Methods Detail
Add(T)
public bool Add(T item)
Parameters
- item
- T
Returns
Implements
Clear()
public void Clear()
Implements
Contains(T)
public bool Contains(T item)
Parameters
- item
- T
Returns
Implements
CopyTo(T[], int)
public void CopyTo(T[] array, int arrayIndex)
Parameters
- array
- T[]
- arrayIndex
- int
Implements
ExceptWith(IEnumerable<T>)
public void ExceptWith(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Implements
GetEnumerator()
public IEnumerator<T> GetEnumerator()
Returns
Implements
IntersectWith(IEnumerable<T>)
public void IntersectWith(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Implements
IsProperSubsetOf(IEnumerable<T>)
public bool IsProperSubsetOf(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
IsProperSupersetOf(IEnumerable<T>)
public bool IsProperSupersetOf(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
IsSubsetOf(IEnumerable<T>)
public bool IsSubsetOf(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
IsSupersetOf(IEnumerable<T>)
public bool IsSupersetOf(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
Overlaps(IEnumerable<T>)
public bool Overlaps(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
Remove(T)
public bool Remove(T item)
Parameters
- item
- T
Returns
Implements
SetEquals(IEnumerable<T>)
public bool SetEquals(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Returns
Implements
SymmetricExceptWith(IEnumerable<T>)
public void SymmetricExceptWith(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Implements
UnionWith(IEnumerable<T>)
public void UnionWith(IEnumerable<T> other)
Parameters
- other
- IEnumerable<T>
Implements
IntersectWith(LinkedHashSet<T>)
Modifies the current LinkedHashSet<T> object to contain only elements that are present in that object and in the specified LinkedHashSet<T>.
public void IntersectWith(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
UnionWith(LinkedHashSet<T>)
Modifies the current LinkedHashSet<T> object to contain all elements that are present in itself, the specified set, or both.
public void UnionWith(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The collection to compare to the current LinkedHashSet<T> object.
SymmetricExceptWith(LinkedHashSet<T>)
Modifies the current LinkedHashSet<T> object to contain only elements that are present either in that object or in the specified set, but not both.
public void SymmetricExceptWith(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
SetEquals(LinkedHashSet<T>)
Determines whether a LinkedHashSet<T> object and the specified set contain the same elements.
public bool SetEquals(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The collection to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object is equal to
other; otherwise, false
.
Overlaps(LinkedHashSet<T>)
Determines whether the current LinkedHashSet<T> object and a specified set share common elements.
public bool Overlaps(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object and
otherSet
share at least one common element;
otherwise, false
.
IsSubsetOf(LinkedHashSet<T>)
Determines whether a LinkedHashSet<T> object is a subset of the specified set.
public bool IsSubsetOf(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The collection to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object is a subset
of otherSet
; otherwise, false
.
IsSupersetOf(LinkedHashSet<T>)
Determines whether a LinkedHashSet<T> object is a superset of the specified set.
public bool IsSupersetOf(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object is a
superset of otherSet
; otherwise, false
.
IsProperSubsetOf(LinkedHashSet<T>)
Determines whether a LinkedHashSet<T> object is a proper subset of the specified set.
public bool IsProperSubsetOf(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object is a proper
subset of otherSet
; otherwise, false
.
IsProperSupersetOf(LinkedHashSet<T>)
Determines whether a LinkedHashSet<T> object is a proper superset of the specified set.
public bool IsProperSupersetOf(LinkedHashSet<T> otherSet)
Parameters
- otherSet
-
Linked
Hash Set<T>
The set to compare to the current LinkedHashSet<T> object.
Returns
true
if the LinkedHashSet<T> object is a proper
superset of otherSet
; otherwise, false
.
GetNodes()
Gets the backend array of the hash table.
protected IEnumerable<LinkedHashSet<T>.ProtectedNode> GetNodes()
Returns
The backend array.
Explicit Interface Implementations Detail
ICollection<T>.Add(T)
private void ICollection<T>.Add(T item)
Parameters
- item
- T
Implements
IEnumerable.GetEnumerator()
private IEnumerator IEnumerable.GetEnumerator()