Three Dimensional Dictionary in C#

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 will make this whole collection with a Dictionary collection, one of the best data structures for searching efficiency. So our "Three-dimensional dictionary collection" will look like this:

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

Next > Dictionary Tutorial in C# 


Recommended Free Ebook
Similar Articles