HashSet<T> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 HashSet<T> 類別的新執行個體。
多載
HashSet<T>() |
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的預設相等比較子。 |
HashSet<T>(IEnumerable<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的預設相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。 |
HashSet<T>(IEqualityComparer<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的指定相等比較子。 |
HashSet<T>(Int32) |
將 HashSet<T> 類別的新執行個體初始化,該類別為空白,但為 |
HashSet<T>(IEnumerable<T>, IEqualityComparer<T>) |
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的指定相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。 |
HashSet<T>(Int32, IEqualityComparer<T>) |
將 HashSet<T> 類別的新執行個體初始化,該類別對集合類型使用指定的相等比較子,並擁有足夠容量容納 |
HashSet<T>(SerializationInfo, StreamingContext) |
已淘汰.
使用序列化資料,初始化 HashSet<T> 類別的新執行個體。 |
HashSet<T>()
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的預設相等比較子。
public:
HashSet();
public HashSet ();
Public Sub New ()
範例
下列範例示範如何建立和填入兩個 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);
}
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
備註
物件的容量 HashSet<T> 是物件可以保存的項目數目。 物件 HashSet<T> 容量會自動增加,因為專案會新增至 物件。
此建構函式是 O (1) 作業。
適用於
HashSet<T>(IEnumerable<T>)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的預設相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。
public:
HashSet(System::Collections::Generic::IEnumerable<T> ^ collection);
public HashSet (System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.HashSet<'T> : seq<'T> -> System.Collections.Generic.HashSet<'T>
Public Sub New (collection As IEnumerable(Of T))
參數
- collection
- IEnumerable<T>
集合 (Collection),其項目會複製到新的集合 (Set)。
例外狀況
collection
為 null
。
範例
下列範例示範如何從現有的集合建立 HashSet<T> 集合。 在此範例中,會分別使用偶數和奇數整數來建立兩個集合。 接著會從偶數整數集建立第三 HashSet<T> 個物件。
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 }
*/
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
備註
物件的容量 HashSet<T> 是物件可以保存的項目數目。 物件 HashSet<T> 容量會自動增加,因為專案會新增至 物件。
如果 collection
包含重複專案,則集合會包含每個唯一專案的其中一個。 不會擲回例外狀況。 因此,結果集的大小與的大小 collection
不同。
此建構函式是 O (n
) 作業,其中 n
是 參數中的 collection
元素數目。
適用於
HashSet<T>(IEqualityComparer<T>)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
初始化 HashSet<T> 類別的新執行個體,這個執行個體是空白的,並使用集合類型的指定相等比較子。
public:
HashSet(System::Collections::Generic::IEqualityComparer<T> ^ comparer);
public HashSet (System.Collections.Generic.IEqualityComparer<T> comparer);
public HashSet (System.Collections.Generic.IEqualityComparer<T>? comparer);
new System.Collections.Generic.HashSet<'T> : System.Collections.Generic.IEqualityComparer<'T> -> System.Collections.Generic.HashSet<'T>
Public Sub New (comparer As IEqualityComparer(Of T))
參數
- comparer
- IEqualityComparer<T>
在集合中比較各個值時所要使用的 IEqualityComparer<T> 實作;若要針對集合類型使用預設 EqualityComparer<T> 實作,則為 null
。
備註
物件的容量 HashSet<T> 是物件可以保存的項目數目。 物件 HashSet<T> 容量會自動增加,因為專案會新增至 物件。
此建構函式是 O (1) 作業。
適用於
HashSet<T>(Int32)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
將 HashSet<T> 類別的新執行個體初始化,該類別為空白,但為 capacity
項目保留了空間,並對集合類型使用預設相等比較子。
public:
HashSet(int capacity);
public HashSet (int capacity);
new System.Collections.Generic.HashSet<'T> : int -> System.Collections.Generic.HashSet<'T>
Public Sub New (capacity As Integer)
參數
- capacity
- Int32
的初始大小 HashSet<T>。
備註
由於重設大小相當昂貴, (需要重新套用) ,因此這會嘗試根據的值 capacity
設定初始容量來將重設大小的需求降到最低。
適用於
HashSet<T>(IEnumerable<T>, IEqualityComparer<T>)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
初始化 HashSet<T> 類別的新執行個體,這個執行個體使用集合類型的指定相等比較子、包含從指定之集合複製的項目,並具有足以容納所複製項目數的容量。
public:
HashSet(System::Collections::Generic::IEnumerable<T> ^ collection, System::Collections::Generic::IEqualityComparer<T> ^ comparer);
public HashSet (System.Collections.Generic.IEnumerable<T> collection, System.Collections.Generic.IEqualityComparer<T> comparer);
public HashSet (System.Collections.Generic.IEnumerable<T> collection, System.Collections.Generic.IEqualityComparer<T>? comparer);
new System.Collections.Generic.HashSet<'T> : seq<'T> * System.Collections.Generic.IEqualityComparer<'T> -> System.Collections.Generic.HashSet<'T>
Public Sub New (collection As IEnumerable(Of T), comparer As IEqualityComparer(Of T))
參數
- collection
- IEnumerable<T>
集合 (Collection),其項目會複製到新的集合 (Set)。
- comparer
- IEqualityComparer<T>
在集合中比較各個值時所要使用的 IEqualityComparer<T> 實作;若要針對集合類型使用預設 EqualityComparer<T> 實作,則為 null
。
例外狀況
collection
為 null
。
範例
下列範例會使用 提供的 IEqualityComparer<T> ,允許不區分大小寫的車輛類型集合元素 HashSet<T> 的比較。
#using <System.Core.dll>
using namespace System;
using namespace System::Collections::Generic;
ref class Program
{
public:
static void Main()
{
HashSet<String^> ^allVehicles = gcnew HashSet<String^>(StringComparer::OrdinalIgnoreCase);
List<String^>^ someVehicles = gcnew List<String^>();
someVehicles->Add("Planes");
someVehicles->Add("Trains");
someVehicles->Add("Automobiles");
// Add in the vehicles contained in the someVehicles list.
allVehicles->UnionWith(someVehicles);
Console::WriteLine("The current HashSet contains:\n");
for each (String^ vehicle in allVehicles)
{
Console::WriteLine(vehicle);
}
allVehicles->Add("Ships");
allVehicles->Add("Motorcycles");
allVehicles->Add("Rockets");
allVehicles->Add("Helicopters");
allVehicles->Add("Submarines");
Console::WriteLine("\nThe updated HashSet contains:\n");
for each (String^ vehicle in allVehicles)
{
Console::WriteLine(vehicle);
}
// Verify that the 'All Vehicles' set contains at least the vehicles in
// the 'Some Vehicles' list.
if (allVehicles->IsSupersetOf(someVehicles))
{
Console::Write("\nThe 'All' vehicles set contains everything in ");
Console::WriteLine("'Some' vechicles list.");
}
// Check for Rockets. Here the OrdinalIgnoreCase comparer will compare
// true for the mixed-case vehicle type.
if (allVehicles->Contains("roCKeTs"))
{
Console::WriteLine("\nThe 'All' vehicles set contains 'roCKeTs'");
}
allVehicles->ExceptWith(someVehicles);
Console::WriteLine("\nThe excepted HashSet contains:\n");
for each (String^ vehicle in allVehicles)
{
Console::WriteLine(vehicle);
}
// Remove all the vehicles that are not 'super cool'.
allVehicles->RemoveWhere(gcnew Predicate<String^>(&isNotSuperCool));
Console::WriteLine("\nThe super cool vehicles are:\n");
for each (String^ vehicle in allVehicles)
{
Console::WriteLine(vehicle);
}
}
private:
// Predicate to determine vehicle 'coolness'.
static bool isNotSuperCool(String^ vehicle)
{
bool superCool = (vehicle == "Helicopters") || (vehicle == "Motorcycles");
return !superCool;
}
};
int main()
{
Program::Main();
}
// The program writes the following output to the console::
//
// The current HashSet contains:
//
// Planes
// Trains
// Automobiles
//
// The updated HashSet contains:
//
// Planes
// Trains
// Automobiles
// Ships
// Motorcycles
// Rockets
// Helicopters
// Submarines
//
// The 'All' vehicles set contains everything in 'Some' vechicles list.
//
// The 'All' vehicles set contains 'roCKeTs'
//
// The excepted HashSet contains:
//
// Ships
// Motorcycles
// Rockets
// Helicopters
// Submarines
//
// The super cool vehicles are:
//
// Motorcycles
// Helicopters
HashSet<string> allVehicles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
List<string> someVehicles = new List<string>();
someVehicles.Add("Planes");
someVehicles.Add("Trains");
someVehicles.Add("Automobiles");
// Add in the vehicles contained in the someVehicles list.
allVehicles.UnionWith(someVehicles);
Console.WriteLine("The current HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
allVehicles.Add("Ships");
allVehicles.Add("Motorcycles");
allVehicles.Add("Rockets");
allVehicles.Add("Helicopters");
allVehicles.Add("Submarines");
Console.WriteLine("\nThe updated HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Verify that the 'All Vehicles' set contains at least the vehicles in
// the 'Some Vehicles' list.
if (allVehicles.IsSupersetOf(someVehicles))
{
Console.Write("\nThe 'All' vehicles set contains everything in ");
Console.WriteLine("'Some' vechicles list.");
}
// Check for Rockets. Here the OrdinalIgnoreCase comparer will compare
// true for the mixed-case vehicle type.
if (allVehicles.Contains("roCKeTs"))
{
Console.WriteLine("\nThe 'All' vehicles set contains 'roCKeTs'");
}
allVehicles.ExceptWith(someVehicles);
Console.WriteLine("\nThe excepted HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Remove all the vehicles that are not 'super cool'.
allVehicles.RemoveWhere(isNotSuperCool);
Console.WriteLine("\nThe super cool vehicles are:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Predicate to determine vehicle 'coolness'.
bool isNotSuperCool(string vehicle)
{
bool superCool = (vehicle == "Helicopters") || (vehicle == "Motorcycles");
return !superCool;
}
// The program writes the following output to the console.
//
// The current HashSet contains:
//
// Planes
// Trains
// Automobiles
//
// The updated HashSet contains:
//
// Planes
// Trains
// Automobiles
// Ships
// Motorcycles
// Rockets
// Helicopters
// Submarines
//
// The 'All' vehicles set contains everything in 'Some' vechicles list.
//
// The 'All' vehicles set contains 'roCKeTs'
//
// The excepted HashSet contains:
//
// Ships
// Motorcycles
// Rockets
// Helicopters
// Submarines
//
// The super cool vehicles are:
//
// Motorcycles
// Helicopters
Imports System.Collections.Generic
Class Program
Public Shared Sub Main()
Dim allVehicles As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
Dim someVehicles As New List(Of String)()
someVehicles.Add("Planes")
someVehicles.Add("Trains")
someVehicles.Add("Automobiles")
' Add in the vehicles contained in the someVehicles list.
allVehicles.UnionWith(someVehicles)
Console.WriteLine("The current HashSet contains:" + Environment.NewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
allVehicles.Add("Ships")
allVehicles.Add("Motorcycles")
allVehicles.Add("Rockets")
allVehicles.Add("Helicopters")
allVehicles.Add("Submarines")
Console.WriteLine(Environment.NewLine + "The updated HashSet contains:" + Environment.NewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
' Verify that the 'All Vehicles' set contains at least the vehicles in
' the 'Some Vehicles' list.
If allVehicles.IsSupersetOf(someVehicles) Then
Console.Write(Environment.NewLine + "The 'All' vehicles set contains everything in ")
Console.WriteLine("'Some' vechicles list.")
End If
' Check for Rockets. Here the OrdinalIgnoreCase comparer will compare
' True for the mixed-case vehicle type.
If allVehicles.Contains("roCKeTs") Then
Console.WriteLine(Environment.NewLine + "The 'All' vehicles set contains 'roCKeTs'")
End If
allVehicles.ExceptWith(someVehicles)
Console.WriteLine(Environment.NewLine + "The excepted HashSet contains:" + Environment.NewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
' Remove all the vehicles that are not 'super cool'.
allVehicles.RemoveWhere(AddressOf isNotSuperCool)
Console.WriteLine(Environment.NewLine + "The super cool vehicles are:" + Environment.NewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
End Sub
' Predicate to determine vehicle 'coolness'.
Private Shared Function isNotSuperCool(vehicle As String) As Boolean
Dim notSuperCool As Boolean = _
(vehicle <> "Helicopters") And (vehicle <> "Motorcycles")
Return notSuperCool
End Function
End Class
'
' The program writes the following output to the console.
'
' The current HashSet contains:
'
' Planes
' Trains
' Automobiles
'
' The updated HashSet contains:
'
' Planes
' Trains
' Automobiles
' Ships
' Motorcycles
' Rockets
' Helicopters
' Submarines
'
' The 'All' vehicles set contains everything in 'Some' vechicles list.
'
' The 'All' vehicles set contains 'roCKeTs'
'
' The excepted HashSet contains:
'
' Ships
' Motorcycles
' Rockets
' Helicopters
' Submarines
'
' The super cool vehicles are:
'
' Motorcycles
' Helicopters
備註
物件的容量 HashSet<T> 是物件可以保存的項目數目。 物件 HashSet<T> 容量會自動增加,因為專案會新增至 物件。
如果 collection
包含重複專案,則集合會包含每個唯一專案的其中一個。 不會擲回例外狀況。 因此,結果集的大小與的大小 collection
不同。
此建構函式是 O (n
) 作業,其中 n
是 參數中的 collection
元素數目。
適用於
HashSet<T>(Int32, IEqualityComparer<T>)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
將 HashSet<T> 類別的新執行個體初始化,該類別對集合類型使用指定的相等比較子,並擁有足夠容量容納 capacity
元素。
public:
HashSet(int capacity, System::Collections::Generic::IEqualityComparer<T> ^ comparer);
public HashSet (int capacity, System.Collections.Generic.IEqualityComparer<T>? comparer);
public HashSet (int capacity, System.Collections.Generic.IEqualityComparer<T> comparer);
new System.Collections.Generic.HashSet<'T> : int * System.Collections.Generic.IEqualityComparer<'T> -> System.Collections.Generic.HashSet<'T>
Public Sub New (capacity As Integer, comparer As IEqualityComparer(Of T))
參數
- capacity
- Int32
的初始大小 HashSet<T>。
- comparer
- IEqualityComparer<T>
在集合中比較各個值時所要使用的 IEqualityComparer<T> 實作;或針對集合類型使用預設 IEqualityComparer<T> 實作,則為 Null (Visual Basic 為 Nothing)。
備註
由於重設大小相當昂貴, (需要重新套用) ,因此這會嘗試根據的值 capacity
設定初始容量來將重設大小的需求降到最低。
適用於
HashSet<T>(SerializationInfo, StreamingContext)
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
- 來源:
- HashSet.cs
警告
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
使用序列化資料,初始化 HashSet<T> 類別的新執行個體。
protected:
HashSet(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected HashSet (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected HashSet (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Generic.HashSet<'T> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.HashSet<'T>
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Generic.HashSet<'T> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.HashSet<'T>
Protected Sub New (info As SerializationInfo, context As StreamingContext)
參數
- info
- SerializationInfo
SerializationInfo 物件,包含序列化 HashSet<T> 物件所需的資訊。
- context
- StreamingContext
StreamingContext 結構,包含與 HashSet<T> 物件相關聯之已序列化資料流的來源和目的地。
- 屬性
備註
還原串行化期間會呼叫這個建構函式,以重新建構透過數據流傳輸的物件。 如需詳細資訊,請參閱 < XML 和 SOAP 序列化。