HashSet w C# z przykładami
W języku C# HashSet jest nieuporządkowaną kolekcją unikalnych elementów. Kolekcja ta została wprowadzona w .NET 3.5 . Obsługuje implementację zestawów i wykorzystuje tablicę mieszającą do przechowywania. Ta kolekcja jest kolekcją typu ogólnego i jest zdefiniowana w sekcji System.Kolekcje.Ogólne przestrzeń nazw. Jest powszechnie używany, gdy chcemy zapobiec umieszczaniu zduplikowanych elementów w kolekcji. Wydajność HashSet jest znacznie lepsza w porównaniu z listą.
Ważne punkty związane z HashSet w C#
- Klasa HashSet implementuje metodę Kolekcja I , Inumerowalne , Kolekcja IReadOnly , wrzesień , Inumerowalne , IDeserializacjaWywołanie zwrotne , I Możliwość serializacji interfejsy.
- W HashSet kolejność elementów nie jest zdefiniowana. Nie można sortować elementów HashSet.
- W HashSet elementy muszą być unikalne.
- W HashSet duplikaty elementów są niedozwolone.
- Zapewnia wiele matematycznych operacji na zbiorach, takich jak przecięcie, suma i różnica.
- Pojemność HashSet to liczba elementów, które może pomieścić.
- HashSet to dynamiczna kolekcja, co oznacza, że rozmiar HashSet jest automatycznie zwiększany po dodaniu nowych elementów.
- W HashSet możesz przechowywać tylko elementy tego samego typu.
Jak utworzyć zestaw hashset?
Klasa HashSet zapewnia 7 różnych typów konstruktorów które są używane do tworzenia HashSet, tutaj używamy tylko HashSet() , konstruktor. Aby przeczytać więcej o konstruktorach HashSet, możesz się odwołać C# | Klasa HashSet .
Zestaw skrótów(): Służy do tworzenia instancji klasy HashSet, która jest pusta i używa domyślnej funkcji porównującej równość dla typu zestawu.
Krok 1: Włączać System.Kolekcje.Ogólne przestrzeni nazw w swoim programie za pomocą za pomocą słowo kluczowe:
using System.Collections.Generic;
Krok 2: Utwórz HashSet za pomocą klasy HashSet, jak pokazano poniżej:
HashSet Hashset_name = new HashSet();
Krok 3: Jeśli chcesz dodać elementy do swojego HashSet, użyj Dodać() metoda dodawania elementów do zestawu HashSet. Możesz także przechowywać elementy w swoim zestawie HashSet za pomocą inicjatora kolekcji.
Krok 4: Dostęp do elementów HashSet można uzyskać za pomocą a dla każdego pętla. Jak pokazano w poniższym przykładzie.
Przykład:
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> >mójhash1 => 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);> > }> > }> }> |
Wyjście
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000
Jak usunąć elementy z HashSet?
W HashSet możesz usuwać elementy z HashSet. Klasa HashSet udostępnia trzy różne metody usuwania elementów, a są to następujące metody:
- Usuń(T) : Ta metoda służy do usuwania określonego elementu z obiektu HashSet.
- Usuń Where(predykat) : Ta metoda służy do usuwania wszystkich elementów spełniających warunki zdefiniowane przez określony predykat z kolekcji HashSet.
- Jasne : Ta metoda służy do usuwania wszystkich elementów z obiektu HashSet.
Przykład 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> >mójhash => 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);> > }> }> |
Wyjście
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0
Ustaw operacje
Klasa HashSet udostępnia również pewne metody używane do wykonywania różnych operacji na zbiorach, a są to metody:
- UnionWith(IEnumerable) : Ta metoda służy do modyfikowania bieżącego obiektu HashSet tak, aby zawierał wszystkie elementy, które są obecne w nim samym, określonej kolekcji lub obu.
Przykład:
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> >mójhash1 => 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);> > }> > }> }> |
Wyjście
C C++ C# Java Ruby PHP Perl
- Przecinają się z (IEnumerable) : Ta metoda służy do modyfikowania bieżącego obiektu HashSet tak, aby zawierał tylko elementy, które są obecne w tym obiekcie i w określonej kolekcji.
Przykład:
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> >mójhash1 => 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);> > }> > }> }> |
Wyjście
C++ Java
- Z wyjątkiem(IEnumerable) : Ta metoda służy do usuwania wszystkich elementów w określonej kolekcji z bieżącego obiektu HashSet.
Przykład:
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> >mójhash1 => 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);> > }> > }> }> |
Wyjście
C C# Ruby