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
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 }
*/
let displaySet (collection: HashSet<int>) =
printf "{"
for i in collection do
printf $" {i}"
printfn " }"
let evenNumbers = HashSet<int>()
let oddNumbers = HashSet<int>()
for i = 0 to 4 do
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2) |> ignore
// Populate oddNumbers with just odd numbers.
oddNumbers.Add(i * 2 + 1) |> ignore
printf $"evenNumbers contains {evenNumbers.Count} elements: "
displaySet evenNumbers
printf $"oddNumbers contains {oddNumbers.Count} elements: "
displaySet oddNumbers
// Create a new HashSet populated with even numbers.
let numbers = HashSet<int> evenNumbers
printfn "numbers UnionWith oddNumbers..."
numbers.UnionWith oddNumbers
printf $"numbers contains {numbers.Count} elements: "
displaySet numbers
// 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 }
注解
有关此 API 的详细信息,请参阅 HashSet <T>的补充 API 备注。
构造函数
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> 类的新实例。 |
属性
Capacity |
获取内部数据结构可以保留的元素总数,而无需调整大小。 |
Comparer |
获取用于确定集中值的相等性 IEqualityComparer<T> 对象。 |
Count |
获取集中包含的元素数。 |
方法
显式接口实现
ICollection<T>.Add(T) |
将项添加到 ICollection<T> 对象。 |
ICollection<T>.IsReadOnly |
获取一个值,该值指示集合是否为只读。 |
IEnumerable.GetEnumerator() |
返回循环访问集合的枚举器。 |
IEnumerable<T>.GetEnumerator() |
返回循环访问集合的枚举器。 |