C# の HashSet と例

C# では、HashSet は一意の要素の順序付けされていないコレクションです。このコレクションは以下で紹介されています .NET 3.5 。セットの実装をサポートし、ストレージにハッシュ テーブルを使用します。このコレクションはジェネリック タイプのコレクションであり、以下で定義されています。 System.Collections.Generic 名前空間。これは通常、重複した要素がコレクションに配置されるのを防ぎたい場合に使用されます。 HashSet のパフォーマンスは、リストと比較してはるかに優れています。

C#のHashSetに関する重要なポイント

  • HashSet クラスは、 Iコレクション IEnumerable IReadOnlyコレクション 9月 IEnumerable IDデシリアライゼーションコールバック 、 そして Iシリアル化可能 インターフェース。
  • HashSet では、要素の順序は定義されていません。 HashSet の要素をソートすることはできません。
  • HashSet では、要素は一意である必要があります。
  • HashSet では、重複した要素は許可されません。
  • これは、交差、和集合、差分などの多くの数学的集合演算を提供します。
  • HashSet の容量は、保持できる要素の数です。
  • HashSet は動的なコレクションであるため、新しい要素が追加されると HashSet のサイズが自動的に増加します。
  • HashSet では、同じ種類の要素のみを格納できます。

ハッシュセットを作成するには?

HashSet クラスが提供するのは、 7 種類のコンストラクター HashSet の作成に使用されます。ここではのみ使用します ハッシュセット() 、コンストラクター。 HashSet のコンストラクターの詳細については、以下を参照してください。 C# |ハッシュセットクラス

ハッシュセット(): これは、空で、セット型のデフォルトの等価比較子を使用する HashSet クラスのインスタンスを作成するために使用されます。

ステップ1: 含む System.Collections.Generic の助けを借りてプログラム内の名前空間を作成する を使用して キーワード:

using System.Collections.Generic; 

ステップ2: 以下に示すように、HashSet クラスを使用して HashSet を作成します。

HashSet Hashset_name = new HashSet(); 

ステップ 3: HashSet に要素を追加したい場合は、次を使用します。 追加() HashSet に要素を追加するメソッド。また、コレクション初期化子を使用して要素を HashSet に保存することもできます。

ステップ 4: HashSet の要素には、 フォーリーチ ループ。以下の例に示すように。

例:

C#




// 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 =>> // 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 =>> 100,1000,10000,100000};> > > // Display elements of myhash2> > Console.WriteLine(> 'Elements of myhash2:'> );> > foreach> (> var> value> in> myhash2)> > {> > Console.WriteLine(value);> > }> > }> }>

出力

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

HashSet から要素を削除するにはどうすればよいですか?

HashSet では、HashSet から要素を削除できます。 HashSet クラスには、要素を削除するための 3 つの異なるメソッドが用意されています。メソッドは次のとおりです。

  • 削除(T) : このメソッドは、HashSet オブジェクトから指定された要素を削除するために使用されます。
  • RemoveWhere(述語) : このメソッドは、指定された述語によって定義された条件に一致するすべての要素を HashSet コレクションから削除するために使用されます。
  • クリア : このメソッドは、HashSet オブジェクトからすべての要素を削除するために使用されます。

例 1:

C#




// 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 =>>' // Add the elements in HashSet> > // Using Add method> > myhash.Add(> 'C'> );> > myhash.Add(> 'C++'> );> > myhash.Add(> 'C#'> );> > myhash.Add(> 'Java'> );> > myhash.Add(> 'Ruby'> );> > // Before using Remove method> > Console.WriteLine(> 'Total number of elements present (Before Removal)'> +> > ' in myhash: {0}'> , myhash.Count);> > // Remove element from HashSet> > // Using Remove method> > myhash.Remove(> 'Ruby'> );> > // After using Remove method> > Console.WriteLine(> 'Total number of elements present (After Removal)'> +> > ' 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#




// 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 =>> // 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 =>> // 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#




// 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 =>> // 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 =>>' // 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#




// 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 =>> // 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 =>> // 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