C# HashSet类介绍和用法示例

2021年5月9日16:28:30 发表评论 806 次浏览

一种HashSet <T>是无序集合独特元素。它下System.Collections.Generic命名空间。它用于我们要防止将重复项插入到集合中的情况。就性能而言, 与列表相比更好。

HashSet类的特征:

  • HashSet <T>类提供了高性能的集合操作。集合是不包含重复元素且其元素没有特定顺序的集合。
  • HashSet的容量<T> object是对象可以容纳的元素数。
  • 哈希集<T>将元素添加到对象后, 对象的容量会自动增加。
  • 哈希集<T>集合未排序, 并且不能包含重复的元素。
  • 哈希集<T>提供了许多数学集合运算, 例如集合加法(联合)和集合减法。

构造器

构造器 描述
HashSet() 初始化为空的HashSet类的新实例, 并使用默认的相等比较器作为集合类型。
HashSet(IEnumerable) 初始化HashSet类的新实例, 该实例使用默认的相等比较器作为集合类型, 包含从指定集合中复制的元素, 并具有足够的容量来容纳复制的元素数量。
HashSet(IEnumerable, IEqualityComparer) 初始化HashSet类的新实例, 该实例使用指定的相等比较器作为集合类型, 包含从指定集合复制的元素, 并具有足够的容量来容纳复制的元素数量。
HashSet(IEqualityComparer) 初始化为空的HashSet类的新实例, 并将指定的相等比较器用于集合类型。
HashSet(Int32) 初始化为HashSet类的新实例, 该实例为空, 但为容量项目保留了空间, 并使用默认的相等比较器作为集合类型。
HashSet(Int32, IEqualityComparer) 初始化HashSet类的新实例, 该实例使用指定的相等比较器作为集合类型, 并具有足够的容量来容纳容量元素。
HashSet(SerializationInfo, StreamingContext) 使用序列化数据初始化HashSet类的新实例。

例子:

//C# code to create a HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a HashSet of odd numbers
         HashSet<int> odd = new HashSet<int>();
  
         //Inserting elements in HashSet
         for ( int i = 0; i <5; i++) {
             odd.Add(2 * i + 1);
         }
  
         //Displaying the elements in the HashSet
         foreach ( int i in odd)
         {
             Console.WriteLine(i);
         }
     }
}

输出如下:

1
3
5
7
9

属性

属性 描述
Comparer 获取IEqualityComparer对象, 该对象用于确定集合中值的相等性。
Count 获取集合中包含的元素数量。

例子:

//C# code to get the number of
//elements that are contained in HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a HashSet of integers
         HashSet<int> mySet = new HashSet<int>();
  
         //Inserting elements in HashSet
         for ( int i = 0; i <5; i++) {
             mySet.Add(i * 2);
         }
  
         //To get the number of
         //elements that are contained in HashSet
         Console.WriteLine(mySet.Count);
     }
}

输出如下:

5

方法

方法 描述
Add(T) 将指定的元素添加到集合中。
Clear() 从HashSet对象中删除所有元素。
Contains(T) 确定HashSet对象是否包含指定的元素。
CopyTo() 将HashSet集合的元素复制到数组。
CreateSetComparer() 返回一个IEqualityComparer对象, 该对象可用于HashSet对象的相等性测试。
Equals(对象) 确定指定对象是否等于当前对象。
ExceptWith(IEnumerable) 从当前HashSet对象中移除指定集合中的所有元素。
GetEnumerator() 返回一个遍历HashSet对象的枚举数。
GetHashCode() 用作默认哈希函数。
GetObjectData(SerializationInfo, StreamingContext) 实现ISerializable接口, 并返回序列化HashSet对象所需的数据。
GetType() 获取当前实例的类型。
IntersectWith(IEnumerable) 修改当前HashSet对象, 使其仅包含该对象和指定集合中存在的元素。
IsProperSubsetOf(IEnumerable) 确定HashSet对象是否是指定集合的​​正确子集。
IsProperSupersetOf(IEnumerable) 确定HashSet对象是否是指定集合的​​正确超集。
IsSubsetOf(IEnumerable) 确定HashSet对象是否是指定集合的​​子集。
IsSupersetOf(IEnumerable) 确定HashSet对象是否为指定集合的​​超集。
MemberwiseClone() 创建当前对象的浅表副本。
OnDeserialization(Object) 当反序列化完成时, 实现ISerializable接口并引发反序列化事件。
Overlaps(IEnumerable) 确定当前HashSet对象和指定的集合是否共享公共元素。
Remove(T) 从HashSet对象中删除指定的元素。
RemoveWhere(Predicate) 从HashSet集合中删除所有与指定谓词定义的条件匹配的元素。
SetEquals(IEnumerable) 确定HashSet对象和指定的集合是否包含相同的元素。
SymmetricExceptWith(IEnumerable) 修改当前HashSet对象, 使其仅包含该对象或指定集合中存在的元素, 但不能同时包含两者。
ToString() 返回表示当前对象的字符串。
TrimExcess() 将HashSet对象的容量设置为它包含的元素的实际数量, 并四舍五入为附近的, 特定于实现的值。
TryGetValue(T, T) 在集合中搜索给定值, 并返回找到的相等值(如果有)。
UnionWith(IEnumerable) 修改当前的HashSet对象, 使其包含自身, 指定的集合或同时存在的所有元素。

例子:

//C# code to Check if a HashSet is
//a subset of the specified collection
using System;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a HashSet of integers
         HashSet<int> mySet1 = new HashSet<int>();
  
         //Inserting elements in HashSet
         //mySet1 only contains even numbers less than
         //equal to 10
         for ( int i = 1; i <= 5; i++)
             mySet1.Add(2 * i);
  
         //Creating a HashSet of integers
         HashSet<int> mySet2 = new HashSet<int>();
  
         //Inserting elements in HashSet
         //mySet2 contains all numbers from 1 to 10
         for ( int i = 1; i <= 10; i++)
             mySet2.Add(i);
  
         //Check if a HashSet mySet1 is a subset
         //of the HashSet mySet2
         Console.WriteLine(mySet1.IsSubsetOf(mySet2));
     }
}

输出如下:

True

例子:

//C# code to check if a HashSet
//contains the specified element
using System;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a HashSet of strings
         HashSet<string> mySet = new HashSet<string>();
  
         //Inserting elements in HashSet
         mySet.Add( "DS" );
         mySet.Add( "C++" );
         mySet.Add( "Java" );
         mySet.Add( "JavaScript" );
  
         //Check if a HashSet contains
         //the specified element
         if (mySet.Contains( "Java" ))
             Console.WriteLine( "Required Element is present" );
         else
             Console.WriteLine( "Required Element is not present" );
     }
}

输出如下:

Required Element is present

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: