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> 未排序,并且不能包含重复的元素。 如果顺序或元素重复比应用程序的性能更重要,请考虑将 List<T> 类与 Sort 方法一起使用。
HashSet<T> 提供了许多数学集运算,例如集加法 () 和集减法的并集。 下表列出了提供的 HashSet<T> 运算及其数学等效项。
HashSet 操作 | 数学等效项 |
---|---|
UnionWith | 联合或集添加 |
IntersectWith | 交叉 口 |
ExceptWith | 设置减法 |
SymmetricExceptWith | 对称差异 |
除了列出的集操作之外, HashSet<T> 类还提供了用于确定集相等性、集重叠以及集是另一个集的子集还是超集的方法。
仅.NET Framework:对于非常大HashSet<T>的对象,可以通过在运行时环境中将配置元素的 <gcAllowVeryLargeObjects>
属性设置为 enabled
,将 64 位系统上的最大容量增加到 true
20 亿个元素。
从 .NET Framework 4 开始, HashSet<T> 类实现 ISet<T> 接口。
HashSet 和 LINQ Set 运算
LINQ 提供对Distinct
实现 IEnumerable 或 IQueryable 接口的任何数据源的 、 Intersect
Union
和 Except
设置操作的访问。 HashSet<T> 提供更大、更可靠的集操作集合。 例如, HashSet<T> 提供比较,例如 IsSubsetOf 和 IsSupersetOf。
LINQ 集操作和 HashSet<T> 操作之间的主要区别在于,LINQ 集操作始终返回新 IEnumerable<T> 集合,而 HashSet<T> 等效的方法会修改当前集合。
通常,如果必须创建新集,或者应用程序只需要访问提供的集操作,则对任何 IEnumerable<T> 集合或数组使用 LINQ set 操作就足够了。 但是,如果应用程序需要访问其他集操作,或者不需要或不需要创建新集合,请使用 HashSet<T> 类。
下表显示了这些 HashSet<T> 操作及其等效的 LINQ set 操作。
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() |
返回循环访问集合的枚举数。 |
扩展方法
适用于
另请参阅
反馈
提交和查看相关反馈