HashSet<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示一組值。
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::ISet<T>
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::IReadOnlySet<T>, System::Collections::Generic::ISet<T>, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::ISet<T>, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::ISet<T>, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class HashSet : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::ISet<T>
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlySet<T>, System.Collections.Generic.ISet<T>, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.ISet<T>, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.ISet<T>, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class HashSet<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.ISet<T>
type HashSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
type HashSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
interface IReadOnlySet<'T>
interface IDeserializationCallback
interface ISerializable
type HashSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ISet<'T>
interface IDeserializationCallback
interface ISerializable
[<System.Serializable>]
type HashSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
[<System.Serializable>]
type HashSet<'T> = class
interface ISerializable
interface IDeserializationCallback
interface ISet<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
[<System.Serializable>]
type HashSet<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
interface ISet<'T>
interface IReadOnlyCollection<'T>
type HashSet<'T> = class
interface ISet<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
[<System.Serializable>]
type HashSet<'T> = class
interface ICollection<'T>
interface ISerializable
interface IDeserializationCallback
interface ISet<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IEnumerable
Public Class HashSet(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IReadOnlyCollection(Of T), ISet(Of T)
Public Class HashSet(Of T)
Implements ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), IReadOnlyCollection(Of T), IReadOnlySet(Of T), ISerializable, ISet(Of T)
Public Class HashSet(Of T)
Implements ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), IReadOnlyCollection(Of T), ISerializable, ISet(Of T)
Public Class HashSet(Of T)
Implements ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), ISerializable
Public Class HashSet(Of T)
Implements ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), ISerializable, ISet(Of T)
Public Class HashSet(Of T)
Implements ICollection(Of T), IEnumerable(Of T), ISet(Of T)
類型參數
- T
雜湊集中項目的類型。
- 繼承
-
HashSet<T>
- 屬性
- 實作
範例
下列範例示範如何合併兩個不同的集合。 本範例會建立兩 HashSet<T> 個 物件,並分別填入偶數和奇數。 第三 HashSet<T> 個物件是從包含偶數的集合建立。 然後,此範例會 UnionWith 呼叫 方法,將奇數集新增至第三個集合。
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
DisplaySet(oddNumbers);
// Create a new HashSet populated with even numbers.
HashSet<int> numbers = new HashSet<int>(evenNumbers);
Console.WriteLine("numbers UnionWith oddNumbers...");
numbers.UnionWith(oddNumbers);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
void DisplaySet(HashSet<int> collection)
{
Console.Write("{");
foreach (int i in collection)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" }");
}
/* This example produces output similar to the following:
* evenNumbers contains 5 elements: { 0 2 4 6 8 }
* oddNumbers contains 5 elements: { 1 3 5 7 9 }
* numbers UnionWith oddNumbers...
* numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
*/
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim evenNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
Dim oddNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
For i As Integer = 0 To 4
' Populate evenNumbers with only even numbers.
evenNumbers.Add(i * 2)
' Populate oddNumbers with only odd numbers.
oddNumbers.Add((i * 2) + 1)
Next i
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count)
DisplaySet(evenNumbers)
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count)
DisplaySet(oddNumbers)
' Create a new HashSet populated with even numbers.
Dim numbers As HashSet(Of Integer) = New HashSet(Of Integer)(evenNumbers)
Console.WriteLine("numbers UnionWith oddNumbers...")
numbers.UnionWith(oddNumbers)
Console.Write("numbers contains {0} elements: ", numbers.Count)
DisplaySet(numbers)
End Sub
Private Shared Sub DisplaySet(ByVal collection As HashSet(Of Integer))
Console.Write("{")
For Each i As Integer In collection
Console.Write(" {0}", i)
Next i
Console.WriteLine(" }")
End Sub
End Class
' This example produces output similar to the following:
' evenNumbers contains 5 elements: { 0 2 4 6 8 }
' oddNumbers contains 5 elements: { 1 3 5 7 9 }
' numbers UnionWith oddNumbers...
' numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
備註
類別 HashSet<T> 提供高效能集合作業。 集合是一個集合,其中包含沒有重複的專案,而且其元素沒有特定順序。
注意
HashSet<T>會 IReadOnlyCollection<T> 實作從 .NET Framework 4.6 開始的介面;在舊版的 .NET Framework 中,類別 HashSet<T> 並未實作此介面。
物件的容量 HashSet<T> 是物件可以保存的元素數目。 物件 HashSet<T> 容量會自動增加,因為專案會加入物件中。
類別 HashSet<T> 是以數學集合的模型為基礎,並提供類似于存取 或 Hashtable 集合索引鍵的 Dictionary<TKey,TValue> 高效能集合作業。 簡單來說, HashSet<T> 類別可以視為 Dictionary<TKey,TValue> 不含值的集合。
HashSet<T>集合未排序,而且不能包含重複的專案。 如果順序或元素重複比應用程式的效能更重要,請考慮搭配 Sort 方法使用 List<T> 類別。
HashSet<T> 提供許多數學集運算,例如 (聯集) 和設定減法。 下表列出提供的 HashSet<T> 作業及其數學對等專案。
HashSet 作業 | 數學對等專案 |
---|---|
UnionWith | 聯集或設定加法 |
IntersectWith | 交叉 口 |
ExceptWith | 設定減法 |
SymmetricExceptWith | 對稱差異 |
除了列出的集合作業之外,類別 HashSet<T> 也提供方法來判斷集合相等、集合重迭,以及集合是否為另一組的子集或超集合。
僅.NET Framework:對於非常大 HashSet<T> 的物件,您可以將 64 位系統上的最大容量增加到 200 億個元素,方法是在執行時間環境中將組態元素的 <gcAllowVeryLargeObjects>
屬性設定 enabled
為 true
。
從 .NET Framework 4 開始,類別 HashSet<T> 會實作 ISet<T> 介面。
HashSet 和 LINQ 設定作業
LINQ 可在實作 或 介面的任何資料來源上存取 Distinct
、 Union
Intersect
和 Except
設定作業。 IQueryableIEnumerable HashSet<T> 提供較大型且更強固的集合集合作業。 例如, HashSet<T> 提供 和 IsSupersetOf 等 IsSubsetOf 比較。
LINQ set 作業和 HashSet<T> 作業的主要差異在於 LINQ set 作業一律會傳回新的 IEnumerable<T> 集合,而對等方法則 HashSet<T> 修改目前的集合。
一般而言,如果您必須建立新的集合,或者如果您的應用程式只需要存取提供的集合作業,則在任何 IEnumerable<T> 集合或陣列上使用 LINQ set 作業就已足夠。 不過,如果您的應用程式需要存取其他集合作業,或不需要建立新的集合,請使用 類別 HashSet<T> 。
下表顯示 HashSet<T> 作業及其相等的 LINQ 集合作業。
HashSet 作業 | LINQ 對等專案 |
---|---|
UnionWith | Union |
IntersectWith | Intersect |
ExceptWith | Except |
未提供。 | Distinct |
SymmetricExceptWith | 未提供。 |
Overlaps | 未提供。 |
IsSubsetOf | 未提供。 |
IsProperSubsetOf | 未提供。 |
IsSupersetOf | 未提供。 |
IsProperSupersetOf | 未提供。 |
SetEquals | 未提供。 |
建構函式
HashSet<T>() |
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的預設相等比較子。 |
HashSet<T>(IEnumerable<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的預設相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。 |
HashSet<T>(IEnumerable<T>, IEqualityComparer<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的指定相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。 |
HashSet<T>(IEqualityComparer<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的指定相等比較子。 |
HashSet<T>(Int32) |
將 HashSet<T> 類別的新執行個體初始化,該類別為空白,但為 |
HashSet<T>(Int32, IEqualityComparer<T>) |
將 HashSet<T> 類別的新執行個體初始化,該類別對集合類型使用指定的相等比較子,並擁有足夠容量容納 |
HashSet<T>(SerializationInfo, StreamingContext) |
已過時。
使用序列化資料,初始化 HashSet<T> 類別的新執行個體。 |
屬性
Comparer |
取得 IEqualityComparer<T> 物件,這個物件可用來判斷集合中的值是否相等。 |
Count |
取得集合中包含的項目數。 |
方法
明確介面實作
ICollection<T>.Add(T) |
將項目加入 ICollection<T> 物件。 |
ICollection<T>.IsReadOnly |
取得值,指出集合是否為唯讀。 |
IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
IEnumerable<T>.GetEnumerator() |
傳回逐一查看集合的列舉值。 |