C#中的HashSet介绍与用法示例

2021年3月25日11:52:09 发表评论 2,844 次浏览

在C#中, HashSet是唯一元素的无序集合。该集合介绍于.NET 3.5。它支持集的实现, 并使用哈希表进行存储。此集合属于通用类型集合, 在以下位置定义System.Collections.Generic命名空间。当我们要防止将重复的元素放置在集合中时, 通常使用它。与列表相比, HashSet的性能要好得多。

重要事项:

  • HashSet类实现了ICollection, IEnumerable, IReadOnlyCollection, 我设置, IEnumerable, IDeserializationCallback和可序列化接口。
  • 在HashSet中, 未定义元素的顺序。你无法对HashSet的元素进行排序。
  • 在HashSet中, 元素必须是唯一的。
  • 在HashSet中, 不允许重复的元素。
  • 提供了许多数学设置操作, 例如交集, 并集和差。
  • HashSet的容量是它可以容纳的元素数。
  • HashSet是一个动态集合, 这意味着在添加新元素时, HashSet的大小会自动增加。
  • 在HashSet中, 你只能存储相同类型的元素。

如何创建一个HashSet?

HashSet类提供7种不同类型的构造函数用于创建HashSet的, 这里我们仅使用HashSet(), 构造函数。要了解有关HashSet的构造函数的更多信息, 请参阅C#| HashSet类别.

HashSet():它用于创建HashSet类的实例, 该实例为空, 并使用默认的相等比较器作为集合类型。

第1步:包括System.Collections.Generic在程序的帮助下命名空间使用关键词:

using System.Collections.Generic;

第2步:使用HashSet类创建一个HashSet, 如下所示:

HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();

第三步:如果要在HashSet中添加元素, 请使用加()在HashSet中添加元素的方法。你还可以使用集合初始化程序将元素存储在HashSet中。

步骤4:HashSet的元素通过使用前言循环。如下例所示。

例子:

// C# program to illustrate how to
// create hashset
using System;
using System.Collections.Generic;
  
class GFG {
  
     // Main Method
     static public void Main()
     {
  
         // Creating HashSet
         // Using HashSet class
         HashSet< string > myhash1 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash1.Add( "C" );
         myhash1.Add( "C++" );
         myhash1.Add( "C#" );
         myhash1.Add( "Java" );
         myhash1.Add( "Ruby" );
         Console.WriteLine( "Elements of myhash1:" );
  
         // Accessing elements of HashSet
         // Using foreach loop
         foreach ( var val in myhash1)
         {
             Console.WriteLine(val);
         }
  
         // Creating another HashSet
         // using collection initializer
         // to initialize HashSet
         HashSet< int > myhash2 = new HashSet< int >() {10, 100, 1000, 10000, 100000};
                  
         // Display elements of myhash2
         Console.WriteLine( "Elements of myhash2:" );
         foreach ( var valu in myhash2)
         {
             Console.WriteLine(valu);
         }
     }
}

输出如下:

Elements of myhash1:
C
C++
C#
Java
Ruby
Elements of myhash2:
10
100
1000
10000
100000

如何从HashSet中删除元素?

在HashSet中, 允许你从HashSet中删除元素。 HashSet <T>类提供了三种不同的方法来删除元素, 这些方法是:

  • 删除(T):此方法用于从HashSet对象中删除指定的元素。
  • RemoveWhere(谓词):此方法用于从HashSet集合中删除所有与指定谓词定义的条件相匹配的元素。
  • 明确:此方法用于从HashSet对象中删除所有元素。

范例1:

// C# program to illustrate how to
// remove elements of HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
     // Main Method
     static public void Main()
     {
  
         // Creating HashSet
         // Using HashSet class
         HashSet< string > myhash = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash.Add( "C" );
         myhash.Add( "C++" );
         myhash.Add( "C#" );
         myhash.Add( "Java" );
         myhash.Add( "Ruby" );
  
         // After using Remove method
         Console.WriteLine( "Total number of elements present" +
                             " in myhash: {0}" , myhash.Count);
  
         // Remove element from HashSet
         // Using Remove method
         myhash.Remove( "Ruby" );
  
         // Before using Remove method
         Console.WriteLine( "Total number of elements present" +
                             " in myhash: {0}" , myhash.Count);
  
         // Remove all elements from HashSet
         // Using Clear method
         myhash.Clear();
         Console.WriteLine( "Total number of elements present" +
                              " in myhash:{0}" , myhash.Count);
     }
}

输出如下:

Total number of elements present in myhash: 5
Total number of elements present in myhash: 4
Total number of elements present in myhash:0

设定作业

HashSet类还提供了一些用于对集合执行不同操作的方法, 这些方法是:

UnionWith(IEnumerable)

:

此方法用于修改当前的HashSet对象, 以包含其本身, 指定的集合或这两者中都存在的所有元素。

例子:

// C# program to illustrate set operations
using System;
using System.Collections.Generic;
  
class GFG {
  
     static public void Main()
     {
  
         // Creating HashSet
         // Using HashSet class
         HashSet< string > myhash1 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash1.Add( "C" );
         myhash1.Add( "C++" );
         myhash1.Add( "C#" );
         myhash1.Add( "Java" );
         myhash1.Add( "Ruby" );
  
         // Creating another HashSet
         // Using HashSet class
         HashSet< string > myhash2 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash2.Add( "PHP" );
         myhash2.Add( "C++" );
         myhash2.Add( "Perl" );
         myhash2.Add( "Java" );
  
         // Using UnionWith method
         myhash1.UnionWith(myhash2);
         foreach ( var ele in myhash1)
         {
             Console.WriteLine(ele);
         }
     }
}

输出如下:

C
C++
C#
Java
Ruby
PHP
Perl

IntersectWith(IEnumerable)

:

此方法用于修改当前HashSet对象, 使其仅包含该对象和指定集合中存在的元素。

例子:

// C# program to illustrate set operations
using System;
using System.Collections.Generic;
  
class GFG {
  
     // Main Method
     static public void Main()
     {
  
         // Creating HashSet
         // Using HashSet class
         HashSet< string > myhash1 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash1.Add( "C" );
         myhash1.Add( "C++" );
         myhash1.Add( "C#" );
         myhash1.Add( "Java" );
         myhash1.Add( "Ruby" );
  
         // Creating another HashSet
         // Using HashSet class
         HashSet< string > myhash2 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash2.Add( "PHP" );
         myhash2.Add( "C++" );
         myhash2.Add( "Perl" );
         myhash2.Add( "Java" );
  
         // Using IntersectWith method
         myhash1.IntersectWith(myhash2);
         foreach ( var ele in myhash1)
         {
             Console.WriteLine(ele);
         }
     }
}

输出如下:

C++
Java

ExceptWith(IEnumerable)

:

此方法用于从当前HashSet对象中删除指定集合中的所有元素。

例子:

// C# program to illustrate set operations
using System;
using System.Collections.Generic;
  
class GFG {
  
     // Main Method
     static public void Main()
     {
  
         // Creating HashSet
         // Using HashSet class
         HashSet< string > myhash1 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash1.Add( "C" );
         myhash1.Add( "C++" );
         myhash1.Add( "C#" );
         myhash1.Add( "Java" );
         myhash1.Add( "Ruby" );
  
         // Creating another HashSet
         // Using HashSet class
         HashSet< string > myhash2 = new HashSet< string >();
  
         // Add the elements in HashSet
         // Using Add method
         myhash2.Add( "PHP" );
         myhash2.Add( "C++" );
         myhash2.Add( "Perl" );
         myhash2.Add( "Java" );
  
         // Using ExceptWith method
         myhash1.ExceptWith(myhash2);
         foreach ( var ele in myhash1)
         {
             Console.WriteLine(ele);
         }
     }
}

输出如下:

C
C#
Ruby

木子山

发表评论

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