logo

HashSet în C# cu exemple

În C#, HashSet este o colecție neordonată de elemente unice. Această colecție este introdusă în .NET 3.5 . Acceptă implementarea de seturi și folosește tabelul hash pentru stocare. Această colecție este de tip generic și este definită în Sistem.Colecții.Generic spatiu de nume. Este folosit în general atunci când dorim să împiedicăm plasarea elementelor duplicate în colecție. Performanța HashSet-ului este mult mai bună în comparație cu lista.

Puncte importante legate de HashSet în C#

  • Clasa HashSet implementează IColecție , IEnumerabil , IReadOnlyCollection , Sept , IEnumerabil , IDserializationCallback , și ISerializabil interfețe.
  • În HashSet, ordinea elementului nu este definită. Nu puteți sorta elementele HashSet.
  • În HashSet, elementele trebuie să fie unice.
  • În HashSet, elementele duplicat nu sunt permise.
  • Oferă multe operații matematice de set, cum ar fi intersecția, unirea și diferența.
  • Capacitatea unui HashSet este numărul de elemente pe care le poate conține.
  • Un HashSet este o colecție dinamică înseamnă că dimensiunea HashSet-ului crește automat atunci când sunt adăugate elemente noi.
  • În HashSet, puteți stoca doar același tip de elemente.

Cum se creează un HashSet?

Clasa HashSet oferă 7 tipuri diferite de constructori care sunt folosite pentru a crea un HashSet, aici le folosim doar HashSet() , constructor. Pentru a citi mai multe despre constructorii lui HashSet, vă puteți referi la C# | Clasa HashSet .

HashSet(): Este folosit pentru a crea o instanță a clasei HashSet care este goală și utilizează comparatorul de egalitate implicit pentru tipul de set.



Pasul 1: Include Sistem.Colecții.Generic namespace din programul dvs. cu ajutorul lui folosind cuvânt cheie:

using System.Collections.Generic;>

Pasul 2: Creați un HashSet folosind clasa HashSet, așa cum se arată mai jos:

HashSet Hashset_name = new HashSet();>

Pasul 3: Dacă doriți să adăugați elemente în HashSet, atunci utilizați Adăuga() metoda de a adăuga elemente în HashSet. De asemenea, puteți stoca elemente în HashSet folosind inițializatorul de colecție.

Pasul 4: Elementele HashSet sunt accesate folosind a pentru fiecare buclă. După cum se arată în exemplul de mai jos.

Exemplu:

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 =>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> value>in> myhash2)> >{> >Console.WriteLine(value);> >}> >}> }>

>

>

Ieșire

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

Cum să eliminați elemente din HashSet?

În HashSet, aveți voie să eliminați elemente din HashSet. Clasa HashSet oferă trei metode diferite pentru a elimina elemente, iar metodele sunt:

  • Eliminați (T) : Această metodă este folosită pentru a elimina elementul specificat dintr-un obiect HashSet.
  • RemoveWhere(predicat) : Această metodă este folosită pentru a elimina toate elementele care corespund condițiilor definite de predicatul specificat dintr-o colecție HashSet.
  • clar : Această metodă este folosită pentru a elimina toate elementele dintr-un obiect HashSet.

Exemplul 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 =>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'>);> >// 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);> >}> }>

>

alfabet la numere
>

Ieșire

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

Setați operațiuni

Clasa HashSet oferă, de asemenea, câteva metode care sunt utilizate pentru a efectua diferite operații pe seturi, iar metodele sunt:

  • UnionWith(IEnumerable) : Această metodă este utilizată pentru a modifica obiectul HashSet curent pentru a conține toate elementele care sunt prezente în sine, colecția specificată sau ambele.

Exemplu:

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 =>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);> >}> >}> }>

>

>

Ieșire

C C++ C# Java Ruby PHP Perl>
  • IntersectWith(IEnumerable) : Această metodă este folosită pentru a modifica obiectul HashSet curent pentru a conține doar elemente care sunt prezente în acel obiect și în colecția specificată.
    Exemplu:

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 =>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);> >}> >}> }>

>

Neena Gupta
>

Ieșire

C++ Java>
  • ExceptWith(IEnumerable) : Această metodă este folosită pentru a elimina toate elementele din colecția specificată din obiectul HashSet curent.

Exemplu:

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 =>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);> >}> >}> }>

>

>

Ieșire

C C# Ruby>