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 }
注解
该 HashSet<T> 类提供高性能集合运算。 集是一个集合,其中包含不重复的元素,并且其元素没有特定顺序。
对象的容量 HashSet<T> 是对象可以保存的元素数。 随着 HashSet<T> 元素添加到对象中,对象的容量会自动增加。
该HashSet<T>类基于数学集的模型,并提供类似于访问Dictionary<TKey,TValue>或Hashtable集合键的高性能集合操作。 简单而言, HashSet<T> 类可以视为 Dictionary<TKey,TValue> 没有值的集合。
HashSet<T>集合未排序,并且不能包含重复的元素。 如果顺序或元素重复比应用程序的性能更重要,请考虑将 List<T> 类与 Sort 该方法一起使用。
HashSet<T> 提供许多数学集运算,例如集加法(联合)和集减法。 下表列出了提供的 HashSet<T> 运算及其数学等效项。
| HashSet 操作 | 数学等效项 |
|---|---|
| UnionWith | 并集或集合加法 |
| IntersectWith | 路口 |
| ExceptWith | 集合减法 |
| SymmetricExceptWith | 对称差异 |
除了列出的集作之外, HashSet<T> 该类还提供用于确定集相等性、重叠集以及集是另一组的子集还是超集的方法。
HashSet<T> 类实现 ISet<T> 接口。
HashSet 和 LINQ 集合运算
LINQ 提供对任何实现 Distinct 或 Union 接口的数据源的 Intersect、Except、IEnumerable 和 IQueryable 集合操作的访问。
HashSet<T> 提供更大和更强大的集合操作集。 例如, HashSet<T> 提供比较,例如 IsSubsetOf 和 IsSupersetOf。
LINQ 集作和 HashSet<T> 作之间的主要区别是 LINQ 集作始终返回新 IEnumerable<T> 集合,而 HashSet<T> 等效方法修改当前集合。
通常,如果必须创建新集,或者应用程序只需要访问提供的集作,则对任何集合或数组使用 LINQ 集作 IEnumerable<T> 将足够。 但是,如果应用程序需要访问额外的集合操作,或者不需要创建新集合,请使用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>, IEqualityComparer<T>) |
初始化类的新实例,该实例 HashSet<T> 使用集类型的指定相等比较器,包含从指定集合复制的元素,并且有足够的容量来容纳复制的元素数。 |
| HashSet<T>(IEnumerable<T>) |
初始化类的新实例,该实例 HashSet<T> 使用集类型的默认相等比较器,包含从指定集合复制的元素,并且有足够的容量来容纳复制的元素数。 |
| HashSet<T>(IEqualityComparer<T>) |
初始化为空的 HashSet<T> 类的新实例,并为集类型使用指定的相等比较器。 |
| HashSet<T>(Int32, IEqualityComparer<T>) |
初始化类的新实例,该实例 HashSet<T> 使用集类型的指定相等比较器,并且有足够的容量来容纳 |
| HashSet<T>(Int32) |
初始化类的新实例,该实例 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() |
返回循环访问集合的枚举器。 |