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.

C#
public class LinkedHashSet<T> : ISet<T>
where T : notnull

Type Parameters

T
notnull

The type of elements maintained by this set.

Inheritance
LinkedHashSet<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

LinkedHashSet(int, float)

Initializes a new instance of the LinkedHashSet<T> class.

LinkedHashSet(HashTableConstants, int, float)

Initializes a new instance of the LinkedHashSet<T> class.

Fields

DefaultMaxCapacity

The default maximum capacity.

DefaultMaxSize

The default maximum size.

DefaultInitialCapacity

The default initial capacity. This is also the minimum capacity.

DefaultLoadFactor

The default load factor.

Properties

LoadFactor

Gets the load factor.

Count
IsReadOnly
CapacityAndLimit

Gets the capacity and limit.

FirstAndLastNode

Gets the first and last nodes, or null when this set is empty.

Methods

Add(T)
Clear()
Contains(T)
CopyTo(T[], int)
ExceptWith(IEnumerable<T>)
GetEnumerator()
IntersectWith(IEnumerable<T>)
IsProperSubsetOf(IEnumerable<T>)
IsProperSupersetOf(IEnumerable<T>)
IsSubsetOf(IEnumerable<T>)
IsSupersetOf(IEnumerable<T>)
Overlaps(IEnumerable<T>)
Remove(T)
SetEquals(IEnumerable<T>)
SymmetricExceptWith(IEnumerable<T>)
UnionWith(IEnumerable<T>)
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>.

UnionWith(LinkedHashSet<T>)

Modifies the current LinkedHashSet<T> object to contain all elements that are present in itself, the specified set, or both.

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.

SetEquals(LinkedHashSet<T>)

Determines whether a LinkedHashSet<T> object and the specified set contain the same elements.

Overlaps(LinkedHashSet<T>)

Determines whether the current LinkedHashSet<T> object and a specified set share common elements.

IsSubsetOf(LinkedHashSet<T>)

Determines whether a LinkedHashSet<T> object is a subset of the specified set.

IsSupersetOf(LinkedHashSet<T>)

Determines whether a LinkedHashSet<T> object is a superset of the specified set.

IsProperSubsetOf(LinkedHashSet<T>)

Determines whether a LinkedHashSet<T> object is a proper subset of the specified set.

IsProperSupersetOf(LinkedHashSet<T>)

Determines whether a LinkedHashSet<T> object is a proper superset of the specified set.

GetNodes()

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.

C#
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.

C#
protected LinkedHashSet(HashTableConstants constants, int initialCapacity, float loadFactor)

Parameters

constants
HashTableConstants

The constants for the hash table.

initialCapacity
int

The initial capacity.

loadFactor
float

The load factor.

Fields Detail

DefaultMaxCapacity

The default maximum capacity.

C#
public const int DefaultMaxCapacity = 1073741824

Field Value

int

DefaultMaxSize

The default maximum size.

C#
public const int DefaultMaxSize = 2147483647

Field Value

int

DefaultInitialCapacity

The default initial capacity. This is also the minimum capacity.

C#
public const int DefaultInitialCapacity = 16

Field Value

int

DefaultLoadFactor

The default load factor.

C#
public const float DefaultLoadFactor = 0.75

Field Value

Properties Detail

LoadFactor

Gets the load factor.

C#
public float LoadFactor { get; }

Property Value

Count

C#
public int Count { get; }

Property Value

int

Implements

IsReadOnly

C#
public bool IsReadOnly { get; }

Property Value

Implements

CapacityAndLimit

Gets the capacity and limit.

C#
protected (int Capacity, int Limit) CapacityAndLimit { get; }

Property Value

FirstAndLastNode

Gets the first and last nodes, or null when this set is empty.

C#
protected (LinkedHashSet<T>.ProtectedNode First, LinkedHashSet<T>.ProtectedNode Last) FirstAndLastNode { get; }

Property Value

Methods Detail

Add(T)

C#
public bool Add(T item)

Parameters

item
T

Returns

Implements

Clear()

C#
public void Clear()

Implements

Contains(T)

C#
public bool Contains(T item)

Parameters

item
T

Returns

Implements

CopyTo(T[], int)

C#
public void CopyTo(T[] array, int arrayIndex)

Parameters

array
T[]
arrayIndex
int

Implements

ExceptWith(IEnumerable<T>)

C#
public void ExceptWith(IEnumerable<T> other)

Parameters

Implements

GetEnumerator()

C#
public IEnumerator<T> GetEnumerator()

Returns

Implements

IntersectWith(IEnumerable<T>)

C#
public void IntersectWith(IEnumerable<T> other)

Parameters

Implements

IsProperSubsetOf(IEnumerable<T>)

C#
public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

Returns

Implements

IsProperSupersetOf(IEnumerable<T>)

C#
public bool IsProperSupersetOf(IEnumerable<T> other)

Parameters

Returns

Implements

IsSubsetOf(IEnumerable<T>)

C#
public bool IsSubsetOf(IEnumerable<T> other)

Parameters

Returns

Implements

IsSupersetOf(IEnumerable<T>)

C#
public bool IsSupersetOf(IEnumerable<T> other)

Parameters

Returns

Implements

Overlaps(IEnumerable<T>)

C#
public bool Overlaps(IEnumerable<T> other)

Parameters

Returns

Implements

Remove(T)

C#
public bool Remove(T item)

Parameters

item
T

Returns

Implements

SetEquals(IEnumerable<T>)

C#
public bool SetEquals(IEnumerable<T> other)

Parameters

Returns

Implements

SymmetricExceptWith(IEnumerable<T>)

C#
public void SymmetricExceptWith(IEnumerable<T> other)

Parameters

Implements

UnionWith(IEnumerable<T>)

C#
public void UnionWith(IEnumerable<T> other)

Parameters

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>.

C#
public void IntersectWith(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public void UnionWith(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public void SymmetricExceptWith(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool SetEquals(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool Overlaps(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool IsSubsetOf(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool IsSupersetOf(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool IsProperSubsetOf(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
public bool IsProperSupersetOf(LinkedHashSet<T> otherSet)

Parameters

otherSet
LinkedHashSet<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.

C#
protected IEnumerable<LinkedHashSet<T>.ProtectedNode> GetNodes()

Returns

Explicit Interface Implementations Detail

ICollection<T>.Add(T)

C#
private void ICollection<T>.Add(T item)

Parameters

item
T

Implements

IEnumerable.GetEnumerator()

C#
private IEnumerator IEnumerable.GetEnumerator()

Returns

Implements