ARTICLE

Three Dimensional Dictionary in C#

Posted by Hemant Srivastava Articles | Visual C# September 09, 2012
In this article, you will learn how to build a three-dimensional Dictionary structure in C#.
Reader Level:


In this article, you will learn how to build a three-dimensional Dictionary structure in C#.

Suppose we need to build a collection of Postal Codes in which each postal code contains a collection of Cities. Each City is itself a collection of Street Addresses and each Street contains all the residents and their personal records. We are going to make this whole collection with a Dictionary collection which is one of the best data structures for the purpose of searching efficiency. So our "Three dimensional dictionary collection" will look like:

Postal Code -> City –> Street Addresses -> Resident's Records -> SSN, Name, Age, Gender and Occupation

Three-Dimensional-Dictionary-in-Csharp.jpg

In C#, we can implement the preceding collection in the following way:

class ThreeDimensionalDictionaryDemo

    {

        public Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>> PeopleCollection = new Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>>();

 

        public void AssemblePersonRecordByPostalCode()

        {

            try

            {

                // A data record file "RecordsFile.txt" is read.

                StreamReader oStreamReader = new StreamReader("RecordsFile.txt");

                string line;

                string postalCode;

                string city;

                string streetAddress;

                ResidentRecord oRecord;

 

                while ((line = oStreamReader.ReadLine()) != null)

                {

                    // In records file, we keep one person's record in one line and data fields are seperated by TAB

                    // For example:

                    // 933467543    John Smith   24  Male    Business    21093   Timonium    2311 York Rd Suit # 44

 

                    string[] parsedItem = line.Split('\t');

 

                    // Retrieving data fields

                    oRecord.SSN = parsedItem[0];

                    oRecord.Name = parsedItem[1];

                    oRecord.Age = Convert.ToInt16(parsedItem[2]);

                    oRecord.Gender = parsedItem[3];

                    oRecord.Occupation = parsedItem[4];

                    postalCode = parsedItem[5];

                    city = parsedItem[6];

                    streetAddress = parsedItem[7];

 

                    // If we get an already existing Postal Code

                    if (PeopleCollection.ContainsKey(postalCode))

                    {

                        //  If we get an already existing City

                        if (PeopleCollection[postalCode].ContainsKey(city))

                        {

                            //  If  we get an already existing StreetAddress

                            if (PeopleCollection[postalCode][city].ContainsKey(streetAddress))

                            {

                                // Person's record is added into list

                                PeopleCollection[postalCode][city][streetAddress].Add(oRecord);

                            }

                            //  If we get a new StreetAddress

                            else

                            {

                                // Street Address is added and value part is newed up

                                PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());

 

                                // Person's record is added into list

                                PeopleCollection[postalCode][city][streetAddress].Add(oRecord);

                            }

                        }

                        //  If we get a new city 

                        else

                        {

                            // City is added and value part is newed up

                            PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());

 

                            // Street Address is added and value part is newed up

                            PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());

 

                            // Person's record is added into list

                            PeopleCollection[postalCode][city][streetAddress].Add(oRecord);

                        }

                    }

                    // If we get a new postal code

                    else

                    {

                       

                        // Postal code is added and value part is newed up

                        PeopleCollection.Add(postalCode, new Dictionary<string, Dictionary<string, List<ResidentRecord>>>());

 

                        // City is added and value part is newed up

                        PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());

 

                        // Street Address is added and value part is newed up

                        PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());

 

                        // Person's record is added into list

                        PeopleCollection[postalCode][city][streetAddress].Add(oRecord);

                    }

                }

            }

            catch (Exception e)

            {

                throw e;

            }

 

        }                  

    }

Thanks,

Hemant Srivastava
 

Login to add your contents and source code to this article
post comment
     

Thanks guys..

Posted by Hemant Srivastava Sep 10, 2012

good work Hemant.

Posted by Dinesh Beniwal Sep 10, 2012

Thank you, Hemant. This looks like a good sample of hierarchical collections. I think it is important for developers to understand that in the real world, there can be multiple postal codes for cities, at least in the United States. So for the USA, relating Zip codes and cities is more challenging.

Posted by Sam Hobbs Sep 09, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter