2

There are several places on the internet that talk about having multy key dictionary such as in:

Multi-key dictionary in c#?

Or

Multi-key dictionaries (of another kind) in C#?

I am looking for a multi key dictionary that will enable me to retrieve the object that I am looking for as long as I provide just one key. In other words If I where to have:

 // multyKeyDictionary = instance of a multikeydictionary 
 multyKeyDictionary.add(key1, key2, someObject);

then I will like to be able to retrive someObject as:

multyKeyDictionary[key1]; or multyKeyDictionary[key2];

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • See [my question](http://stackoverflow.com/questions/311103/any-implementation-of-mapk1-k2-v-i-e-two-keys) about the same data-structure in Java. I could not find anything in Java and I doubt you will find it in C#. I think your best bet is to roll your own class which contains two dictionaries and keeps them in sync. – Miserable Variable Jun 25 '12 at 23:39
  • 1
    See my implementation of a generic multi-key dictionary here: http://www.aronweiler.com/2009/02/multi-key-generic-dictionary-class-for.html. It does exactly what you're looking for. (my blog is also linked from the two other stackoverflow posts you mentioned. – Aron Nov 06 '12 at 21:55
  • Your question is an exact duplicate of the [link](http://stackoverflow.com/questions/1171913/multi-key-dictionaries-of-another-kind-in-c?lq=1) you attached in the question itself. Voting to close. – nawfal Mar 30 '13 at 17:59

1 Answers1

7

Well, I don't understand why don't you just use regular dictionary and add object twice with different keys.

Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("key1", obj);
dict.Add("key2", obj);

// dict["key1"] == dict["key2"]
nawfal
  • 70,104
  • 56
  • 326
  • 368
Zdeněk Topič
  • 762
  • 4
  • 29
  • Dictionary dict = new Dictionary(); dict.Add("key1", obj); dict.Add("key2", obj); dict["key1"] == dict["key2"] – Zdeněk Topič Jun 25 '12 at 23:33
  • Or even a raw hashtable. As long as your keys implement GetHashCode(). Only issue would be removing an object from the table would require knowing all the keys. – pilotcam Jun 25 '12 at 23:34
  • I will have to add the object twice ok I see what you mean. thanks for the help. – Tono Nam Jun 25 '12 at 23:34
  • But the hash table will be really big. I guess that is my best option though right? – Tono Nam Jun 25 '12 at 23:36
  • 1
    I can't think of anything else. Even if you would develop custom Dictionary class that would implement multiple keys for very same object it would be very same. You can of course implement it by extending Dictionary class or by creating ancestor with method that would add same object to multiple keys via http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.71).aspx – Zdeněk Topič Jun 25 '12 at 23:38
  • @Tono Nam: the hash table of some esoteric multikey dictionary implementation with the behaviour you explained will be at least as big. After all there has to exist some mapping between every key and value. The values you point to are just references to the objects, so they are not stored multiple times, if you mean that by "will be really big". – Philip Daubmeier Jun 25 '12 at 23:42
  • Philip is right. The outcome "size" will be always nearly same, this is just easiest approach – Zdeněk Topič Jun 25 '12 at 23:45
  • This answer is not useful in certain cases where equality is little "differently defined". For eg. consider I have a `Person` class where equality is based on `Name`, and the keys are both `Person` but different kind of Persons (for some reason I want to separate them as key1 and key2). This means "Sam" as key1 is different from "Sam" as key2. I have been into a similar scenario before. A flat horizontal structure works in such cases. Also ideally this is not "the solution" but a workaround. See the duplicate questions in "linked" questions where answers use a "two dictionary" solution. – nawfal Mar 30 '13 at 18:41
  • ... apart from the fact that the `Count` property is always the double of what you normally expect. – nawfal Mar 30 '13 at 19:19