Advertisement
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 | 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. |
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
//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);
}
}
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
//C# Example of HashSet<T>
using System;
using System.Collections.Generic;
class HashSetEx
{
public static void Main(String[] ar)
{
//Creating the first HashSet<T> of type String i.e. HashSet<String>, to hold String objects
HashSet<String> hash1 = new HashSet<String>();
//Calling the Add() method to add String objects to HashSet<String>
hash1.Add("B");
hash1.Add("3");
hash1.Add("G");
hash1.Add("C");
hash1.Add("1");
//Printing the elements of HashSet<String>
Console.WriteLine("The contents of the first HashSet<String>: ");
foreach(string element in hash1)
Console.WriteLine(element);
Console.WriteLine("Size of HashSet<String> = "+ hash1.Count);
//Creating the second HashSet<T> of type String i.e. HashSet<String>, to hold String objects
HashSet<String> hash2 = new HashSet<String>();
//Calling the Add() method to add String objects to the second HashSet<String>
hash2.Add("G");
hash2.Add("C");
hash2.Add("1");
//Printing the elements of the second HashSet<String>
Console.WriteLine("The elements of the second HashSet<String>: ");
foreach(string element in hash2)
Console.WriteLine(element);
//Printing the size of updated HashSet<String>
Console.WriteLine("Size of second HashSet<String> = "+ hash2.Count);
//Checking if the first HashSet<String> is a subset of the seconds HashSet<String>
Console.WriteLine("Is the first HashSet<String> is a subset of the seconds HashSet<String>: " + hash1.IsSubsetOf(hash2));
//Checking if the first HashSet<String> is a superset of the first HashSet<String>
Console.WriteLine("Is the first HashSet<String> is a superset of the seconds HashSet<String>: " + hash1.IsSupersetOf(hash2));
}
}
The contents of the first HashSet<String>:
B
3
G
C
1
Size of HashSet<String> = 5
The elements of the second HashSet<String>:
G
C
1
Size of second HashSet<String> = 3
Is the first HashSet<String> is a subset of the seconds HashSet<String>: False
Is the first HashSet<String> is a superset of the seconds HashSet<String>: True
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement