Hash Set Operators

Have you ever had the need to join elements from one collection to another? Or only show the unique elements from one collection to another? I’ve seen some creative ways of doing it. However using the built in UnionWith, SymmectricExceptWith, and ExceptWith methods can do these things.

HashsetOperationsProgram HashsetOperationsSolution

 
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 various methods that compare elements from hash sets to other collections.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            executeUnionWith();
            executeSymmetricExceptWith();
            executeExceptWith();

            Console.ReadLine();
        }

        // Private methods
        //
        // Method that executes UnionWith on the hash set.
        private static void executeUnionWith()
        {
            Console.WriteLine("----Executing Union With Example----");

            // Creating a list first for this demonstration.    
            List<string> listCities = new List<string>()
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Rome"
            };

            // Creating hash set with cities and add methods.
            HashSet<string> hashSetCities = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Manchester"
            };

            // Executing the UnionWith method on the two collections.
            // Again this modified the collection in line without having to declare a variable.
            hashSetCities.UnionWith(listCities);

            foreach (var item in hashSetCities)
                Console.WriteLine(item);

            Console.WriteLine();
        }

        // Method that executes SymmetricExceptWith on the hash set.
        private static void executeSymmetricExceptWith()
        {
            Console.WriteLine("----Executing Symmetric Except With Example----");

            // Creating a list first for this demonstration.    
            List<string> listCities = new List<string>()
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Rome"
            };

            // Creating hash set with cities and add methods.
            HashSet<string> hashSetCities = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Manchester"
            };

            // Executing the SymmetricExceptWith method on the two collections.
            // Again this modified the collection in line without having to declare a variable.
            hashSetCities.SymmetricExceptWith(listCities);

            foreach (var item in hashSetCities)
                Console.WriteLine(item);

            Console.WriteLine();
        }

        // Method that executes ExceptWith on the hash set.
        private static void executeExceptWith()
        {
            Console.WriteLine("----Executing ExceptWith With Example----");

            // Creating a list first for this demonstration.    
            List<string> listCities = new List<string>()
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Rome"
            };

            // Creating hash set with cities and add methods.
            HashSet<string> hashSetCities = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "New York",
                "Paris",
                "Los Angeles",
                "London",
                "Manchester"
            };

            // Executing the ExceptWith method on the two collections.
            // Again this modified the collection in line without having to declare a variable.
            hashSetCities.ExceptWith(listCities);

            foreach (var item in hashSetCities)
                Console.WriteLine(item);

            Console.WriteLine();
        }
    }
}

Take note that the custom string comparer is the same as other examples because I’m using hash sets here.

Another note is that I’m using variable declarations with these examples. By calling these methods I’m making changes to the collections inline. You can modify if you need to.

The UnionWith method call takes all the elements from one collection, all the elements from another collection, and combines them.

The SymmetricExceptWith method takes all elements from the collections and drops all of the elements that are the same. Leaving only the unique elements from the two collections.

The ExceptWith might look like the SymmectricExceptWith method. The difference is it will just have the elements that are unique in the hashset collection. It won’t include any others from the other collection.

With any of these you can still use the built-in string comparer with .NET or use a custom one.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s