HashSet in C# con esempi
In C#, HashSet è una raccolta non ordinata di elementi univoci. Questa raccolta viene introdotta in .NET 3.5 . Supporta l'implementazione di set e utilizza la tabella hash per l'archiviazione. Questa raccolta è di tipo generico ed è definita in System.Collections.Generic spazio dei nomi. Viene generalmente utilizzato quando vogliamo evitare che vengano inseriti elementi duplicati nella raccolta. Le prestazioni dell'HashSet sono molto migliori rispetto all'elenco.
Punti importanti relativi a HashSet in C#
- La classe HashSet implementa il ICollection , IEnumerabile , Collezione IReadOnly , settembre , IEnumerabile , IDeserializationCallback , E ISerializzabile interfacce.
- In HashSet, l'ordine dell'elemento non è definito. Non è possibile ordinare gli elementi di HashSet.
- In HashSet, gli elementi devono essere univoci.
- In HashSet non sono consentiti elementi duplicati.
- Fornisce molte operazioni matematiche sugli insiemi, come intersezione, unione e differenza.
- La capacità di un HashSet è il numero di elementi che può contenere.
- Un HashSet è una raccolta dinamica: la dimensione dell'HashSet viene automaticamente aumentata quando vengono aggiunti nuovi elementi.
- In HashSet è possibile memorizzare solo lo stesso tipo di elementi.
Come creare un HashSet?
La classe HashSet fornisce 7 diversi tipi di costruttori che vengono utilizzati per creare un HashSet, qui utilizziamo solo SetHash() , costruttore. Per saperne di più sui costruttori di HashSet puoi fare riferimento a C# | Classe HashSet .
HashSet(): Viene utilizzato per creare un'istanza vuota della classe HashSet e utilizza l'operatore di confronto di uguaglianza predefinito per il tipo di set.
Passo 1: Includere System.Collections.Generic spazio dei nomi nel tuo programma con l'aiuto di utilizzando parola chiave:
using System.Collections.Generic;
Passo 2: Crea un HashSet utilizzando la classe HashSet come mostrato di seguito:
HashSet Hashset_name = new HashSet();
Passaggio 3: Se desideri aggiungere elementi al tuo HashSet, utilizza Aggiungere() metodo per aggiungere elementi nel tuo HashSet. E puoi anche memorizzare elementi nel tuo HashSet utilizzando l'inizializzatore di raccolta.
Passaggio 4: È possibile accedere agli elementi di HashSet utilizzando a per ciascuno ciclo continuo. Come mostrato nell'esempio seguente.
Esempio:
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> >miohash1 => 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> >miohash2 => 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);> > }> > }> }> |
Produzione
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000
Come rimuovere elementi dall'HashSet?
In HashSet, puoi rimuovere elementi da HashSet. La classe HashSet fornisce tre diversi metodi per rimuovere elementi e i metodi sono:
- Rimuovi(T) : Questo metodo viene utilizzato per rimuovere l'elemento specificato da un oggetto HashSet.
- RimuoviDove(Predicato) : Questo metodo viene utilizzato per rimuovere tutti gli elementi che soddisfano le condizioni definite dal predicato specificato da una raccolta HashSet.
- Chiaro : Questo metodo viene utilizzato per rimuovere tutti gli elementi da un oggetto HashSet.
Esempio 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> >miohash => 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);> > }> }> |
Produzione
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0
Imposta operazioni
La classe HashSet fornisce anche alcuni metodi utilizzati per eseguire diverse operazioni sui set e i metodi sono:
- UnioneCon(IEnumerable) : Questo metodo viene utilizzato per modificare l'oggetto HashSet corrente in modo che contenga tutti gli elementi presenti in esso stesso, nella raccolta specificata o in entrambi.
Esempio:
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> >miohash1 => 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> >miohash2 => 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);> > }> > }> }> |
Produzione
C C++ C# Java Ruby PHP Perl
- IntersectWith(IEnumerable) : Questo metodo viene utilizzato per modificare l'oggetto HashSet corrente in modo che contenga solo gli elementi presenti in quell'oggetto e nella raccolta specificata.
Esempio:
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> >miohash1 => 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> >miohash2 => 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);> > }> > }> }> |
Produzione
C++ Java
- TranneCon(IEnumerable) : Questo metodo viene utilizzato per rimuovere tutti gli elementi nella raccolta specificata dall'oggetto HashSet corrente.
Esempio:
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> >miohash1 => 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> >miohash2 => 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);> > }> > }> }> |
Produzione
C C# Ruby