简体   繁体   中英

The most efficient way of Adding to a Dictionary within a Dictionary

After completing a task in Java, I have now been required to produce the same results in C# and I am in need of some help. The object that I am using is:

Dictionary<int, Dictionary<String, List<int>>> full_map = new Dictionary<int, Dictionary<String, List<int>>>();

I want to add an entry to the inner dictionary if I already have something stored at the main dictionary's key.

To handle this, I started with the logic,

if (full_map.ContainsKey(int.Parse(pop_cy_st_intrst[0])))
{
    Dictionary<String, List<int>> temp = new Dictionary<String, List<int>>();
    //this is the logic I can't figure out.
}
else
{
    full_map.Add(int.Parse(pop_cy_st_intrst[0]), temp_entry);
}

My thought process for the if statement was to store the existing dictionary in the temp and add the new entry to it. Then, put the updated dictionary back at the key location but it keeps throwing me errors.

I believe this would work:

if (full_map.ContainsKey(int.Parse(pop_cy_st_intrst[0])))
    full_map[int.Parse(pop_cy_st_intrst[0])].Add(innerKeyStr, innerValueList);
else
    full_map.Add(int.Parse(pop_cy_st_intrst[0]), new Dictionary<string, List<int>>());

So if the full_map outer dictionary contains the key, then you access the inner dictionary based on that key and add whatever you want (I don't know if it's pop_cy_st_intrst[0] for the inner key as well, so I just leave that up to you).

If full_map does not contain the key, then you add a new inner dictionary for that key.

If you want to add to the inner dictionary whether or not that inner dictionary existed already, then that last line could be

full_map.Add(int.Parse(pop_cy_st_intrst[0]), new Dictionary<string, List<int>>() { { innerKeyStr, innerValueList } });

Use the index of the main dictionary to access the inner dictionary.

Dictionary<int, Dictionary<String, List<int>>> full_map = 
    new Dictionary<int, Dictionary<String, List<int>>>();

var index = int.Parse("10");

if (full_map.ContainsKey(index))
{
    if (full_map[index] == null)
    {
        full_map[index] = new Dictionary<string, List<int>>();
    }
}
else
{
    full_map.Add(index, new Dictionary<string,List<int>>());
}

full_map[index].Add("Blah", new List<int>());

I see there's been a few answers. Here's the one I was working while they got posted. Comments and such added for clarity.

Dictionary<int, Dictionary<String, List<int>>> full_map = new Dictionary<int, Dictionary<String, List<int>>>();

int key = 4;
if (full_map.ContainsKey(key))
{
    // the main dictionary has an entry

    // temp is the innerdictionary assigned to that key
    var temp = full_map[key];

    // add stuff to the inner dictionary
    temp.Add("string key",new List<int>());

}
else
{
    // the main dictionary does not have the key

    // create an inner dictionary
    var innerDictionary = new Dictionary<string,List<int>>();
    innerDictionary.Add("string key", new List<int>());

    // add it to the map with the key
    full_map.Add(key,innerDictionary);
}

What about building your code using TryGetValue()? One call to review collection will be more efficient...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM