HashSet<T> 類別(System.Collections.Generic) | Microsoft Docs

文章推薦指數: 80 %
投票人數:10人

C# 複製. public class HashSet : System.Collections.Generic. ... 需要存取所提供的設定作業,則在任何集合或陣列上使用LINQ set 作業IEnumerable 將會足夠。

跳到主要內容 已不再支援此瀏覽器。

請升級至MicrosoftEdge,以利用最新功能、安全性更新和技術支援。

下載MicrosoftEdge 其他資訊 目錄 結束焦點模式 語言 閱讀英文 儲存 編輯 共用 Twitter LinkedIn Facebook 電子郵件 WeChat 目錄 語言 語言 閱讀英文 儲存 目錄 閱讀英文 儲存 編輯 Twitter LinkedIn Facebook 電子郵件 WeChat 目錄 HashSet類別 參考 此頁面有所助益嗎? Yes No 還有其他意見反應嗎? 系統會將意見反應傳送給Microsoft:按下[提交]按鈕,您的意見反應將用來改善Microsoft產品和服務。

隱私權原則。

送出 謝謝。

定義 命名空間: System.Collections.Generic 組件:System.Collections.dll 組件:System.Core.dll 組件:netstandard.dll 重要 部分資訊涉及發行前產品,在發行之前可能會有大幅修改。

Microsoft對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

表示一組值。

本文內容 generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Collections::Generic::IReadOnlyCollection,System::Collections::Generic::ISet generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Collections::Generic::IReadOnlyCollection,System::Collections::Generic::IReadOnlySet,System::Collections::Generic::ISet,System::Runtime::Serialization::IDeserializationCallback,System::Runtime::Serialization::ISerializable generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Collections::Generic::IReadOnlyCollection,System::Collections::Generic::ISet,System::Runtime::Serialization::IDeserializationCallback,System::Runtime::Serialization::ISerializable generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Runtime::Serialization::IDeserializationCallback,System::Runtime::Serialization::ISerializable generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Collections::Generic::ISet,System::Runtime::Serialization::IDeserializationCallback,System::Runtime::Serialization::ISerializable generic publicrefclassHashSet:System::Collections::Generic::ICollection,System::Collections::Generic::IEnumerable,System::Collections::Generic::ISet publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.IReadOnlyCollection,System.Collections.Generic.ISet publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.IReadOnlyCollection,System.Collections.Generic.IReadOnlySet,System.Collections.Generic.ISet,System.Runtime.Serialization.IDeserializationCallback,System.Runtime.Serialization.ISerializable publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.IReadOnlyCollection,System.Collections.Generic.ISet,System.Runtime.Serialization.IDeserializationCallback,System.Runtime.Serialization.ISerializable [System.Serializable] publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Runtime.Serialization.IDeserializationCallback,System.Runtime.Serialization.ISerializable [System.Serializable] publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.ISet,System.Runtime.Serialization.IDeserializationCallback,System.Runtime.Serialization.ISerializable [System.Serializable] publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.IReadOnlyCollection,System.Collections.Generic.ISet,System.Runtime.Serialization.IDeserializationCallback,System.Runtime.Serialization.ISerializable publicclassHashSet:System.Collections.Generic.ICollection,System.Collections.Generic.IEnumerable,System.Collections.Generic.ISet typeHashSet=class interfaceICollection interfaceseq interfaceIEnumerable interfaceIReadOnlyCollection interfaceISet typeHashSet=class interfaceICollection interfaceseq interfaceIEnumerable interfaceIReadOnlyCollection interfaceISet interfaceIReadOnlySet interfaceIDeserializationCallback interfaceISerializable typeHashSet=class interfaceICollection interfaceseq interfaceIEnumerable interfaceIReadOnlyCollection interfaceISet interfaceIDeserializationCallback interfaceISerializable [] typeHashSet=class interfaceICollection interfaceseq interfaceIEnumerable interfaceISerializable interfaceIDeserializationCallback [] typeHashSet=class interfaceISerializable interfaceIDeserializationCallback interfaceISet interfaceICollection interfaceseq interfaceIEnumerable [] typeHashSet=class interfaceICollection interfaceseq interfaceIEnumerable interfaceISerializable interfaceIDeserializationCallback interfaceISet interfaceIReadOnlyCollection typeHashSet=class interfaceISet interfaceICollection interfaceseq interfaceIEnumerable [] typeHashSet=class interfaceICollection interfaceISerializable interfaceIDeserializationCallback interfaceISet interfaceIReadOnlyCollection interfaceseq interfaceIEnumerable PublicClassHashSet(OfT) ImplementsICollection(OfT),IEnumerable(OfT),IReadOnlyCollection(OfT),ISet(OfT) PublicClassHashSet(OfT) ImplementsICollection(OfT),IDeserializationCallback,IEnumerable(OfT),IReadOnlyCollection(OfT),IReadOnlySet(OfT),ISerializable,ISet(OfT) PublicClassHashSet(OfT) ImplementsICollection(OfT),IDeserializationCallback,IEnumerable(OfT),IReadOnlyCollection(OfT),ISerializable,ISet(OfT) PublicClassHashSet(OfT) ImplementsICollection(OfT),IDeserializationCallback,IEnumerable(OfT),ISerializable PublicClassHashSet(OfT) ImplementsICollection(OfT),IDeserializationCallback,IEnumerable(OfT),ISerializable,ISet(OfT) PublicClassHashSet(OfT) ImplementsICollection(OfT),IEnumerable(OfT),ISet(OfT) 類型參數 T 雜湊集中項目的類型。

繼承 Object HashSet 屬性 SerializableAttribute 實作 ICollection IEnumerable IReadOnlyCollection ISet IEnumerable IReadOnlySet IDeserializationCallback ISerializable 範例 下列範例示範如何合併兩個不同的集合。

這個範例會建立兩個HashSet物件,並將偶數和奇數分別填入。

第三個HashSet物件是從包含偶數的集合中建立的。

然後,此範例會呼叫UnionWith方法,此方法會將奇數設定為第三個集合。

HashSetevenNumbers=newHashSet(); HashSetoddNumbers=newHashSet(); for(inti=0;i<5;i++) { //Populatenumberswithjustevennumbers. evenNumbers.Add(i*2); //PopulateoddNumberswithjustoddnumbers. oddNumbers.Add((i*2)+1); } Console.Write("evenNumberscontains{0}elements:",evenNumbers.Count); DisplaySet(evenNumbers); Console.Write("oddNumberscontains{0}elements:",oddNumbers.Count); DisplaySet(oddNumbers); //CreateanewHashSetpopulatedwithevennumbers. HashSetnumbers=newHashSet(evenNumbers); Console.WriteLine("numbersUnionWithoddNumbers..."); numbers.UnionWith(oddNumbers); Console.Write("numberscontains{0}elements:",numbers.Count); DisplaySet(numbers); voidDisplaySet(HashSetcollection) { Console.Write("{"); foreach(intiincollection) { Console.Write("{0}",i); } Console.WriteLine("}"); } /*Thisexampleproducesoutputsimilartothefollowing: *evenNumberscontains5elements:{02468} *oddNumberscontains5elements:{13579} *numbersUnionWithoddNumbers... *numberscontains10elements:{0246813579} */ ImportsSystem.Collections.Generic ClassProgram SharedSubMain() DimevenNumbersAsHashSet(OfInteger)=NewHashSet(OfInteger)() DimoddNumbersAsHashSet(OfInteger)=NewHashSet(OfInteger)() ForiAsInteger=0To4 'PopulateevenNumberswithonlyevennumbers. evenNumbers.Add(i*2) 'PopulateoddNumberswithonlyoddnumbers. oddNumbers.Add((i*2)+1) Nexti Console.Write("evenNumberscontains{0}elements:",evenNumbers.Count) DisplaySet(evenNumbers) Console.Write("oddNumberscontains{0}elements:",oddNumbers.Count) DisplaySet(oddNumbers) 'CreateanewHashSetpopulatedwithevennumbers. DimnumbersAsHashSet(OfInteger)=NewHashSet(OfInteger)(evenNumbers) Console.WriteLine("numbersUnionWithoddNumbers...") numbers.UnionWith(oddNumbers) Console.Write("numberscontains{0}elements:",numbers.Count) DisplaySet(numbers) EndSub PrivateSharedSubDisplaySet(ByValcollectionAsHashSet(OfInteger)) Console.Write("{") ForEachiAsIntegerIncollection Console.Write("{0}",i) Nexti Console.WriteLine("}") EndSub EndClass 'Thisexampleproducesoutputsimilartothefollowing: 'evenNumberscontains5elements:{02468} 'oddNumberscontains5elements:{13579} 'numbersUnionWithoddNumbers... 'numberscontains10elements:{0246813579} 備註 HashSet類別可提供高效能的設定作業。

集合是一種集合,其中不包含重複的專案,且其元素沒有特定的順序。

注意 HashSetIReadOnlyCollection從.NETFramework4.6開始,在舊版的.NETFramework中執行介面,HashSet類別未執行這個介面。

物件的容量HashSet是物件可以保存的元素數目。

HashSet當專案加入至物件時,物件的容量會自動增加。

HashSet類別是以數學集合的模型為基礎,並提供類似于存取或集合之索引鍵的高效能設定Dictionary作業Hashtable。

簡單來說,HashSet可以將類別視為Dictionary沒有值的集合。

HashSet集合未排序,且不能包含重複的元素。

如果順序或元素的重複專案比您應用程式的效能更重要,請考慮將List類別與Sort方法搭配使用。

HashSet提供許多數學設定作業,例如設定加法(等位)和設定減法。

下表列出所提供的HashSet作業及其數學對應專案。

HashSet操作 數學相等 UnionWith Union或set加法 IntersectWith 交叉口 ExceptWith 設定減法 SymmetricExceptWith 對稱差異 除了列出的設定作業之外,此HashSet類別也會提供方法來判斷設定的相等性、集合的重迭,以及集合是否為另一個集合的子集或超集合。

僅限.NETFramework:對於非常大的HashSet物件,您可以在enabledtrue執行時間環境中將設定元素的屬性設定為,以增加64位系統上2000000000個元素的最大容量。

從.NETFramework4開始,類別會實HashSet作為ISet介面。

HashSet和LINQ設定作業 LINQ可讓您存取DistinctUnion任何執行IntersectExcept或介面之資料來源上的、和設定IEnumerable作業IQueryable。

HashSet提供更大且更穩固的設定作業集合。

例如,HashSet提供比較,例如IsSubsetOf和IsSupersetOf。

LINQ設定作業和作業之間的主要差異HashSet是,linq設定作業一律會傳回新的IEnumerable集合,而對HashSet等的方法會修改目前的集合。

一般而言,如果您必須建立新的集合,或您的應用程式只需要存取所提供的設定作業,則在任何集合或陣列上使用LINQset作業IEnumerable將會足夠。

但是,如果您的應用程式需要存取額外的設定作業,或是不想要或不需要建立新的集合,請使用HashSet類別。

下表顯示HashSet作業和其對等的LINQ設定作業。

HashSet操作 LINQ對等專案 UnionWith Union IntersectWith Intersect ExceptWith Except 未提供。

Distinct SymmetricExceptWith 未提供。

Overlaps 未提供。

IsSubsetOf 未提供。

IsProperSubsetOf 未提供。

IsSupersetOf 未提供。

IsProperSupersetOf 未提供。

SetEquals 未提供。

建構函式 HashSet() 初始化HashSet類別的新執行個體,這個執行個體是空白的,並使用集合類型的預設相等比較子。

HashSet(IEnumerable) 初始化HashSet類別的新執行個體,這個執行個體使用集合類型的預設相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。

HashSet(IEnumerable,IEqualityComparer) 初始化HashSet類別的新執行個體,這個執行個體使用集合類型的指定相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。

HashSet(IEqualityComparer) 初始化HashSet類別的新執行個體,這個執行個體是空白的,並使用集合類型的指定相等比較子。

HashSet(Int32) 將HashSet類別的新執行個體初始化,該類別為空白,但為capacity項目保留了空間,並對集合類型使用預設相等比較子。

HashSet(Int32,IEqualityComparer) 將HashSet類別的新執行個體初始化,該類別對集合類型使用指定的相等比較子,並擁有足夠容量容納capacity元素。

HashSet(SerializationInfo,StreamingContext) 使用序列化資料,初始化HashSet類別的新執行個體。

屬性 Comparer 取得IEqualityComparer物件,這個物件可用來判斷集合中的值是否相等。

Count 取得集合中包含的項目數。

方法 Add(T) 將指定的項目加入至集合。

Clear() 將所有項目從HashSet物件中移除。

Contains(T) 判斷HashSet物件是否包含指定的項目。

CopyTo(T[]) 將HashSet物件的項目複製到陣列。

CopyTo(T[],Int32) 從指定的陣列索引開始,將HashSet物件的項目複製到陣列。

CopyTo(T[],Int32,Int32) 從指定的陣列索引開始,將HashSet物件的指定項目數複製到陣列。

CreateSetComparer() 傳回IEqualityComparer物件,這個物件可用來進行HashSet物件的相等測試。

EnsureCapacity(Int32) 確保此雜湊集可以包含指定的元素數目,而不需要擴充。

Equals(Object) 判斷指定的物件是否等於目前的物件。

(繼承來源Object) ExceptWith(IEnumerable) 將指定集合中的所有項目從目前的HashSet物件中移除。

GetEnumerator() 傳回會逐一查看HashSet物件的列舉值。

GetHashCode() 做為預設雜湊函式。

(繼承來源Object) GetObjectData(SerializationInfo,StreamingContext) 實作ISerializable介面,並傳回序列化HashSet物件所需的資料。

GetType() 取得目前執行個體的Type。

(繼承來源Object) IntersectWith(IEnumerable) 修改目前的HashSet物件,以便僅包含該物件和指定之集合中同時出現的項目。

IsProperSubsetOf(IEnumerable) 判斷HashSet物件是否為指定之集合的真子集。

IsProperSupersetOf(IEnumerable) 判斷HashSet物件是否為指定之集合的真超集。

IsSubsetOf(IEnumerable) 判斷HashSet物件是否為指定之集合的子集。

IsSupersetOf(IEnumerable) 判斷HashSet物件是否為指定之集合的超集。

MemberwiseClone() 建立目前Object的淺層複製。

(繼承來源Object) OnDeserialization(Object) 實作ISerializable介面,並於還原序列化完成時引發還原序列化事件。

Overlaps(IEnumerable) 判斷目前的HashSet物件與指定的集合是否共用通用項目。

Remove(T) 將指定的項目從HashSet物件中移除。

RemoveWhere(Predicate) 將符合指定述詞所定義之條件的所有項目從HashSet集合中移除。

SetEquals(IEnumerable) 判斷HashSet物件和指定的集合是否包含相同項目。

SymmetricExceptWith(IEnumerable) 修改目前的HashSet物件,以便僅包含該物件或指定之集合(但非兩者)中出現的項目。

ToString() 傳回代表目前物件的字串。

(繼承來源Object) TrimExcess() 將HashSet物件的容量設定為所包含之項目的實際數目,已四捨五入為鄰近的實作特定值。

TryGetValue(T,T) 搜尋指定值的資料集,並傳回所找到的相等值(如果有的話)。

UnionWith(IEnumerable) 修改目前的HashSet物件,以包含該物件和(或)指定之集合中出現的所有項目。

明確介面實作 ICollection.Add(T) 將項目加入ICollection物件。

ICollection.IsReadOnly 取得值,指出集合是否為唯讀。

IEnumerable.GetEnumerator() 傳回逐一查看集合的列舉值。

IEnumerable.GetEnumerator() 傳回逐一查看集合的列舉值。

擴充方法 ToImmutableArray(IEnumerable) 從指定的集合建立不可變的陣列。

ToImmutableDictionary(IEnumerable,Func) 從現有的項目集合建構不可變的字典,將轉換函式套用至來源索引鍵。

ToImmutableDictionary(IEnumerable,Func,IEqualityComparer) 根據序列的某些轉換來建構不可變的字典。

ToImmutableDictionary(IEnumerable,Func,Func) 列舉及轉換序列,並產生其內容的不可變字典。

ToImmutableDictionary(IEnumerable,Func,Func,IEqualityComparer) 列舉及轉換序列,並使用指定的索引鍵比較子產生其內容的不可變字典。

ToImmutableDictionary(IEnumerable,Func,Func,IEqualityComparer,IEqualityComparer) 列舉及轉換序列,並使用指定的索引鍵與值比較子產生其內容的不可變字典。

ToImmutableHashSet(IEnumerable) 列舉序列,並產生其內容之不可變雜湊集。

ToImmutableHashSet(IEnumerable,IEqualityComparer) 列舉序列、產生其內容之不可變雜湊集,且針對集合類型使用指定的相等比較子。

ToImmutableList(IEnumerable) 列舉序列,並產生其內容的不可變清單。

ToImmutableSortedDictionary(IEnumerable,Func,Func) 列舉及轉換序列,並產生不可變的排序字典作為內容。

ToImmutableSortedDictionary(IEnumerable,Func,Func,IComparer) 列舉及轉換序列,並使用指定的索引鍵比較子產生不可變的排序字典作為內容。

ToImmutableSortedDictionary(IEnumerable,Func,Func,IComparer,IEqualityComparer) 列舉及轉換序列,並使用指定的索引鍵與值比較子產生不可變的排序字典作為內容。

ToImmutableSortedSet(IEnumerable) 列舉序列,並產生其內容的不可變排序資料集。

ToImmutableSortedSet(IEnumerable,IComparer) 列舉序列、產生其內容的不可變排序資料集,並使用指定的比較子。

CopyToDataTable(IEnumerable) 根據輸入DataTable物件(其中泛型參數T為DataRow)傳回包含IEnumerable物件複本的DataRow。

CopyToDataTable(IEnumerable,DataTable,LoadOption) 根據輸入DataRow物件(其中泛型參數T為DataTable),將IEnumerable物件複製到指定的DataRow。

CopyToDataTable(IEnumerable,DataTable,LoadOption,FillErrorEventHandler) 根據輸入DataRow物件(其中泛型參數T為DataTable),將IEnumerable物件複製到指定的DataRow。

Aggregate(IEnumerable,Func) 將累加函式套用到序列上。

Aggregate(IEnumerable,TAccumulate,Func) 將累加函式套用到序列上。

使用指定的初始值做為初始累加值。

Aggregate(IEnumerable,TAccumulate,Func,Func) 將累加函式套用到序列上。

使用指定的值做為初始累加值,並使用指定的函式來選取結果值。

All(IEnumerable,Func) 判斷序列的所有項目是否全都符合條件。

Any(IEnumerable) 判斷序列是否包含任何項目。

Any(IEnumerable,Func) 判斷序列的任何項目是否符合條件。

Append(IEnumerable,TSource) 將值附加在序列結尾。

AsEnumerable(IEnumerable) 傳回IEnumerable類型的輸入。

Average(IEnumerable,Func) 計算在輸入序列中各項目上叫用轉換函式後所取得之Decimal值序列的平均值。

Average(IEnumerable,Func) 計算在輸入序列中各項目上叫用轉換函式後所取得之Double值序列的平均值。

Average(IEnumerable



請為這篇文章評分?