Comparing HashSets Example

This is a simple example of showing how you can see if two collections, especially hashsets, are equal in their elements.

HashSetCompareSolution HashSetCompareProgram

 

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySets
{
    class Program
    {
        /// <summary>
        /// This program demonstrates the ability to compare a hash set collection to another collection type.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            var bigCities = createBigCityHashSet();
            var usCities = createUsCitiesHashSet();
            var citiesArray = createCitiesArray();

            // Using the SetEquals method to see if the collections are equal.
            // Note that the order of the elements does not matter when comparing the hash set to the array.
            bool areEqual = bigCities.SetEquals(usCities);
            bool isEqual = bigCities.SetEquals(citiesArray);

            // Displaying the results of the comparisons.
            Console.WriteLine("bigCities = usCities? {0}", areEqual);
            Console.WriteLine("bigCities = citiesArray? {0}", isEqual);

            Console.ReadLine();
        }

        // Private Methods
        //
        public static HashSet<string> createBigCityHashSet()
        {
            HashSet<string> cities = new HashSet<string>
            {
                "New York", "Los Angeles", "San Diego", "Paris", "Dallas"
            };
            return cities;
        }

        public static HashSet<string> createUsCitiesHashSet()
        {
            HashSet<string> cities = new HashSet<string>
            {
                "Phoenix", "Odessa", "Wichita", "Denver", "Billings"
            };
            return cities;
        }

        public static string[] createCitiesArray()
        {
            string[] cities = new string[]
            {
                "San Diego", "Los Angeles", "Dallas", "Paris", "New York"
            };
            return cities;
        }
    }
}

This is a really simpe program example. So I won’t get into the details because you can figure it out looking at the code.

First you’ll notice that I’m making a comparision with the two hash sets seeing if they’re equal. I’m also doing the same only with a hash set and an array. This is so I can show you that you can do with more than one collection type. And also it doesn’t matter what order the elements are in because of the nature of the hashset.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s