Advertisement



< Prev
Next >



C# HashSet<T> Class





HashSet<T> is a collection class that implements Set Interface. HashSet<T> stores its element in a hash table by a process called hashing. In hashing, hash code of every element is computed internally and the value of hash code determines the index at which the element is stored in HashSet<T>. Hence, the order of elements in HashSet<T> is unknown and cannot be a guaranteed.

Note: HashSet<T> implements the ICollection<T>, ISet<T>, IEnumerable, IEnumerable<T>, ISerializable and IDeserializationCallback interfaces and it is defined in the System.Collection.Generic namespace.




Important points about HashSet<T> Collection class.






Constructors of HashSet<T>:


Constructors Description
HashSet<T>()
This constructor of HashSet<T> creates an empty HashSet<T> and has the default initial capacity.

The capacity of an HashSet<T> is the number of elements it can hold, as the new elements are added to the HashSet<T>, its capacity is automatically increased.

HashSet<T>(IEnumerable<T>)
This constructor creates an HashSet<T> initialized with the elements of an IEnumerable<T> collection.






Properties of HashSet<T>:


Properties Description
Count
This property gives total number of elements in an HashSet<T>.

ICollection.IsSynchronzied
This property determines whether an ICollection is thread-safe or not.

ICollection.IsReadOnly
Thie property gets a value indicating whether a collection is read-only.

Comparer
This property returns a IEqualityComparer<T> object that is used to determine equality for the values in the set.






Some commonly used methods of HashSet<T> class


.

.

Methods Description
bool Add()
This method adds an element to the HashSet<T> and returns true if element was successfully added or false, if the element was already present in the HashSet<T>.

bool Contains(T object)
This method checks if HashSet<T> contains a specific object.

void Clear()
This method removes all of the elements from the HashSet<T>.

HashSet<T>.Enumerator GetEnumerator()
This method returns to enumerate the entire HashSet<T>.

bool Remove(T object)
This method removes the first occurrence of an object from the HashSet<T>, if it is present.

bool IsSubsetOf(IEnumerable<T>)
This method determines whether a HashSet<> is a subset of the specified collection.

bool IsSupersetOf(IEnumerable<T>)
This method determines whether a HashSet<> is a superset of the specified collection.

void IntersectWith(IEnumerable<T>)
This method modifies the current HashSet<T> to contain only elements that are present in that object and in the specified collection.

void UnionWith(IEnumerable<T>)
This method modifies the current HashSet<T> object to contain all elements that are present in itself, the specified collection, or both.




Advertisement




Creating a HashSet<T> and displaying its features.


In this program we will create a HashSet<T> to hold String objects and we will add/remove some elements from this set and use it to initialize a new HashSet<T> from the existing HashSet<>.

//C# Example of HashSet<T>

using System;
using System.Collections.Generic;

class HashSetEx
{
public static void Main(String[] ar)
{
	//Creating a HashSet<T> of type String i.e. HashSet<String>, to hold String objects
	HashSet<String> hash= new HashSet<String>();
	
	//Calling the Add() method to add String objects to HashSet<String>
	hash.Add("B");
	hash.Add("3");
	hash.Add("G");
	hash.Add("C");
	hash.Add("1");

	//Printing the elements of HashSet<String>
	Console.WriteLine("The contents of HashSet<String>: ");
	foreach(string element in hash)
		Console.WriteLine(element);
	Console.WriteLine("Size of HashSet<String> = "+ hash.Count);

	//Removing an element G from HashSet<String>
	//By calling the Remove() method
	hash.Remove("G");

	//Printing the elements of HashSet<String>
	Console.WriteLine("After removing element G, HashSet<String>: ");
	foreach(string element in hash)
		Console.WriteLine(element);

	//Printing the size of updated HashSet<String>
	Console.WriteLine("Size of updated HashSet<String> = "+ hash.Count);

	//Creating new HashSet<String> from existing HashSet<String>
	HashSet<String> hash2 = new HashSet<String>(hash);


	//Printing the elements of HashSet<String>
	Console.WriteLine("The contents of new HashSet<String> created from the first HashSet<String>: ");
	foreach(string element in hash2)
		Console.WriteLine(element);

	//Printing the size of last created HashSet<String>
	Console.WriteLine("Size of the newly created HashSet<String> = "+ hash.Count);
}
}


Output is - :


The contents of HashSet<String>:
B
3
G
C
1
Size of HashSet<String> = 5
After removing element G, HashSet<String>:
B
3
C
1
Size of updated HashSet<String> = 4
The contents of new HashSet<String> created from the first HashSet<String>:
B
3
C
1
Size of the newly created HashSet<String> = 4


Program Analysis







To determine if one HashSet<T> is a subset or superset of another HashSet<T>.


In the upcoming example, we are going to call the IsSubsetOf(IEnumerable<T>) and the IsSupersetOf(IEnumerable<T>) method of the HashSet<T> class, where:

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement