Thursday, February 5, 2015

Hashsets

Intro:

Do you use data within arrays/lists and think to yourself: is there another datatype for my unique (yes unique) data that's fairly efficient and easy to write?  Hashsets for the win!

Coding:

Lets write some code!  Time to use my time honored tradition of fanstasy names, we'll go for uniqueness using the names of the dwarves from the most recent Hobbit movie series.  Forgive the lazy way of loading up the initial hashset, lets see what hashsets can do for you:

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

namespace HashSetSample
{
  class Program
  {
    static void Main(string[] args)
    {
      // step 1: create our hashsets!
      HashSet<string> dwarvenHash = new HashSet<string>();
      HashSet<string> darkHairedHash = new HashSet<string>();
      
      // step 2: load our hashsets!
      dwarvenHash.Add("Dwalin");
      dwarvenHash.Add("Balin");
      dwarvenHash.Add("Kili");
      dwarvenHash.Add("Fili");
      dwarvenHash.Add("Dori");
      dwarvenHash.Add("Nori");
      dwarvenHash.Add("Ori");
      dwarvenHash.Add("Oin");
      dwarvenHash.Add("Gloin");
      dwarvenHash.Add("Bifur");
      dwarvenHash.Add("Bofur");
      dwarvenHash.Add("Bombur");
      dwarvenHash.Add("Thorin");

      darkHairedHash.Add("Dwalin");
      darkHairedHash.Add("Kili");
      darkHairedHash.Add("Fili");
      darkHairedHash.Add("Nori");
      darkHairedHash.Add("Ori");
      darkHairedHash.Add("Gloin");
      darkHairedHash.Add("Bifur");
      darkHairedHash.Add("Bofur");
      darkHairedHash.Add("Thorin");

      string names = string.Empty;
      foreach (string dwarf in dwarvenHash.ToArray())
        names += dwarf + " ";
      names.Trim();
      // step 3: show initial data.
      Console.WriteLine("Name of all dwarves in the party : " + names);
      names = string.Empty;
      foreach (string dwarf in darkHairedHash.ToArray())
        names += dwarf + " ";
      names.Trim();
      Console.WriteLine("Name of dwarves with dark hair : " + names);

      // step 4: remove some data.
      dwarvenHash.ExceptWith(darkHairedHash);

      // step 5: show modified data.
      names = string.Empty;
      foreach (string dwarf in dwarvenHash.ToArray())
        names += dwarf + " ";
      names.Trim();
      Console.WriteLine("Name of dwarves with lighter/gray hair : " + names);


      Console.WriteLine("\nPress Any Key to Exit.");
      Console.ReadKey();
    }
  }
}

Lets discuss the fun of above.  Step 1: creating your HashSet.  For my blog purpose the data type is string.  Step 2: loading the data.  There are more elegant ways, search the interwebz and reply down below with your attempts :).  Step 3: displaying the initial data of each hashset to verify they loaded the way you'd expected them to.  Step 4 is a method available to us to remove any size of data that exists from a larger size of data that exists.  AKA: I want to remove all darkHairedHash dwarves from my list of dwarvenHash dwarves.  In a very elegant (and very efficient) way of doing that, I'm using ExceptWith.  Step 5 is showing off the modified data to verify any/all changes were made.

Our result:













Huzzah!  As always any/all comments are appreciated.

Source:

M$ HashSets

No comments:

Post a Comment