HashSet in C#, Hashet is an unordered collection of unique elements. This collection is introduced in .NET 3.5. It supports the implementation of sets and uses the hash table for storage. This collection is of the generic type collection and it is defined under System.Collections.Generic namespace. It will be generally used whenever want to prevent duplicate elements from being placed in the collection. The performance of the HashSet is much better in comparison to the list.
Important Points:
- The HashSet class implements the ICollection, IEnumerable, IReadOnlyCollection, ISet, IEnumerable, IDeserializationCallback, and ISerializable interfaces.
- In HashSet, the order of the element is not defined. You cannot sort the elements of HashSet.
- In HashSet, the elements must be unique.
- In HashSet, duplicate elements are not allowed.
- It will be provides many mathematical set operations, such as intersection, union, and difference.
- The capacity of a HashSet is the number of elements it can hold.
- A HashSet is a dynamic collection means the size of the HashSet is automatically increased when the new elements are added.
- In HashSet, you can only store the same type of elements.
How to create a HashSet?
The HashSet class provides 7 different types of constructors which are used to create a HashSet, here we only use HashSet(), constructor.
HashSet(): It is used to create an instance of the HashSet class that is empty and uses the default equality comparer for the set type.
Step 1: Include System.Collections.Generic namespace in your program with the help of using keyword:
using System.Collections.Generic;
Step 2: Create a HashSet using the HashSet class as shown below:
HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();
Step 3: If you want to add elements in your HashSet, then use Add() method to add elements in your HashSet. And you can also store elements in your HashSet using collection initializer.
Step 4: The elements of HashSet is accessed by using a foreach loop. As shown in the below example.
Example 1:
// 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); } } }
Output:
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000
How to remove elements from the HashSet?
In HashSet, you are allowed to remove elements from the HashSet. HashSet<T> class provides three different methods to remove elements and the methods are:
- Remove(T): This method is used to remove the specified element from a HashSet object.
- RemoveWhere(Predicate): This method is used to remove all elements that match the conditions defined by the specified predicate from a HashSet collection.
- Clear: This method is used to remove all elements from a HashSet object.
Example 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"); // 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); } }
Output:
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0
Set Operations
In this HashSet class also provides some methods that are used to perform different operations on sets and the methods are:
- UnionWith(IEnumerable): This method is used to modify the current HashSet object to contain all elements that are present in itself, the specified collection, or both.
Example :1
// 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); } } }
Output:
C C++ C# Java Ruby PHP Perl
- IntersectWith(IEnumerable): This method is used to modify the current HashSet object to contain only elements that are present in that object and in the specified collection.
Example :2
// 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); } } }
Output:
C++ Java
- ExceptWith(IEnumerable): This method is used to remove all elements in the specified collection from the current HashSet object.
Example :3
// 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); } } }
Output:
C C# Ruby
Recent Comments