Set Comparisons And Subsets Example

This example is another simple one. I’m showing how the IsSubsetOf IsSuperSet methods of the HashSet collection class works.

ComparisonSolution ComparisonProgram

 

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 demonstrates set comparisons and subsets.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            var noFrillsBigCities = bigUsCities();
            var regularUsCities = usCities();
            var bigUnitedStatesCities = bigCities();

            // Figuring out which collection is a subset.
            Console.WriteLine("****Displaying IsSubsetOf Results****");
            bool noFrillsIsSubset = noFrillsBigCities.IsSubsetOf(bigUnitedStatesCities);
            Console.WriteLine("noFrillsBigCities subset of bigUnitedStatesCities? {0}", noFrillsIsSubset);

            bool bigUsIsSubset = bigUnitedStatesCities.IsSubsetOf(regularUsCities);
            Console.WriteLine("bigUnitedStatesCities subset of regularUsCities? {0}", bigUsIsSubset);

            bool regularIsSubset = regularUsCities.IsSubsetOf(bigUnitedStatesCities);
            Console.WriteLine("regularUsCities subset of bigUnitesStatesCities? {0}", regularIsSubset);
            Console.WriteLine();

            
            // Figuring out which collection is a super set..
            Console.WriteLine("****Displaying IsSuperSet Results****");
            bool noFrillsIsSuperset = noFrillsBigCities.IsSubsetOf(bigUnitedStatesCities);
            Console.WriteLine("noFrillsBigCities superset of bigUnitedStatesCities? {0}", noFrillsIsSuperset);

            bool bigUsIsSuperset = bigUnitedStatesCities.IsSubsetOf(regularUsCities);
            Console.WriteLine("bigUnitedStatesCities super set of regularUsCities? {0}", bigUsIsSuperset);

            bool regularIsSuperset = regularUsCities.IsSubsetOf(bigUnitedStatesCities);
            Console.WriteLine("regularUsCities superset of bigUnitesStatesCities? {0}", regularIsSuperset);
            Console.WriteLine();

            Console.ReadLine();
        }

        // Private Methods
        //
        public static HashSet<string> bigUsCities()
        {
            HashSet<string> cities = new HashSet<string>
            {
                "San Diego", "Dallas"
            };
            return cities;
        }

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

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

Honestly I don’t see much of this used much anymore. Not with the ability to LINQ now. Here are some examples of how to use the IsSubsetOf method on hash collections. You can call on these if you don’t want to use LINQ or are designing your own class. If anything it’s a good historical note to see how things could’ve been done before LINQ was created. Enjoy!!! 🙂

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