I'm looking for a data structure similar to a dictionary that returns the set of all related items to a key.
For example, I would use it like this:
var data = new FancyDataStructure();
data.Add(new string[] {"Elizabeth", "Liz", "Betty"});
data.Add(new string[] {"Bob", "Robert", "Rob"});
string[] alternateNames1 = data["Betty"];
string[] alternateNames2 = data["Liz"]
In this instance, alternateNames1 would be an array containing "Liz" and "Elizabeth", and alternateNames2 would be an array containing "Elizabeth" and "Betty."
I don't want to reinvent this, but I couldn't find any examples of such a structure.
Update
Thank you to those that have written back with suggestions. Many people have suggested using some version of Dictionary<string, IEnumerable<string>>
. Currently I am using this approach, but it doesn't actually fulfill the requirement without being horribly difficult to maintain. Every value in every list needs to be able to function as a key to every other value ever added to it in a set.
Thus, given the following:
data.Add(new string[] {"Elizabeth", "Liz"}
data.Add(new string[] {"Liz", "Betty"}
alternates = data["Betty"];
I would expect alternates to now contain "Elizabeth," and "Liz."
It looks as though I might just have to build such a structure to suit my needs. Keep the ideas coming though!
Brian