HASHSET COLLECTION FOR UNIQUE UNSORTED ITEMS


HashSet is the new collection class introduced in .NET3.5. It can be used to store
a single type. But compare to equivalent List type, it keep
1.Unique elements without duplication.
2.Unsorted elements using Hash algorithm.
3.Because of above behaviour, it has better performance.
Below example describes the details of the comparison mentioned above
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("one");
HashSet<string> h1 = new HashSet<string>();
h1.Add("one");
h1.Add("two");
h1.Add("one");
Once you run this code, List will be filled with 3 items compare to HashSet, which avoided the duplicate item of "one". So now you understood that HashSet will skip equal items. But how HashSet comparing different items for equivality.

Equality logic in HashSet

Two types of equality settings available in HashSet
1.Default, which one you already seen above like duplicate item "one" skipped by HashSet.
2.Customized. We can customize the logic for checking equality. Below explained one real time scenario and implementation.Scenario is that you have various clients like IBM,MS,CA etc...They frequenty sending alerts to you on daily basis. But you need to see the latest alerts only. So if IBM sent more than 1 alerts, you want to take latest only. You want to store all latest alerts from each client in a HashSet and this HashSet needs to skip the old alerts from same client. Here you need to implement the Equality logic of HasTable as a customized. Keep the logic in a separte class as below.

Custom Equality Checking Class

class ClientComparer : IEqualityComparer<string>
    {
        public ClientComparer() { }
        public bool Equals(string x, string y)
        {
            bool returnValue = false;
            if (x.Contains("IBM") == true && y.Contains("IBM") == true)
            {
                returnValue = true;
            }
            else if(x.Contains("MS") == true && y.Contains("MS") == true)
            {
                returnValue = true;
            }
            return returnValue;
    
   }
      //Not so relevant in this scenario. So just returning 0
        public int GetHashCode(string x)
        {
            return 0;
        }
    }
Usage of Implemented Customized Eqauality Checking
string[] messages = {"Alert from IBM Today", "Alert from MS Today", "Alert from IBM
Yesterday", "Alert from MS Yesterday", "Alert from CA Today" };
                HashSet<string> h = new HashSet<string>(new ClientComparer());
                List<string> l = new List<string>();
                foreach (string message in messages)
                {
                    h.Add(message);
                }
                GridView1.DataSource = h;
                GridView1.DataBind();
You can see that duplicate alerts not included in output. This specific scenario
will work only if alert messages are keeping with latest on top. But my aim is to
create a scenario and explain things. Please go with more functionalities available
in this new collection type.
 
Ebook Download
View all
Learn
View all