Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What do you call a collection in which each item may be looked up by one of multiple keys? A partial, simplified definition in C# might be:

class MyDataStructure<TKey1, TKey2, TValue> {
    private List<TValue> _values;
    //map keys to indices
    private Dictionary<TKey1, int> _keys1;
    private Dictionary<TKey2, int> _keys2;

    public TValue GetByKey1(TKey1 key) ...
    public TValue GetByKey2(TKey2 key) ...
}

There's MultiKeyMap in Java, but that seems to lookup items based on a combination of keys, not by each key individually. Does what I've described have a well-established name?

EDIT

If there's not an established name, feel free to offer a new one. Or state your preference for one of these:

  1. ChoiceKeyDictionary
  2. AlternateKeyDictionary
  3. DualKeyDictionary
share|improve this question

closed as primarily opinion-based by MichaelT, gnat, Kilian Foth, GlenH7, thorsten müller Jan 14 at 15:48

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

    
It would be nice to know in the end what did you choose :) –  nawfal Jan 13 at 13:00
add comment

3 Answers

I've heard the term "multi-key dictionary" used to describe this type of data structure.

Edit: Related questions have been asked on this topic:

http://stackoverflow.com/questions/1171812/multi-key-dictionary-in-c
http://stackoverflow.com/questions/1171913/multi-key-dictionaries-of-another-kind-in-c

share|improve this answer
    
That sounds analogous to MultiKeyMap. Is there a name that would distinguish it from such a collection, which uses combination keys? –  Daniel Dec 13 '11 at 22:54
    
Both questions you reference relate to combination keys. The collection in question is distinct from that. Values may only be looked up by one key at a time; it's either/or. –  Daniel Dec 13 '11 at 22:57
    
I think I understand what you want: a data structure that supports multiple keys, but only uses one of these keys at a time to access related data. Perhaps this requirement warrants a new name for this type of data structure. "Shared-key dictionary" perhaps? –  Bernard Dec 13 '11 at 23:07
    
I'm okay with making up a name. I actually think "multi-key map" is more suitable as a name for this structure. The Java one should be called compound- or combination- key, IMO. –  Daniel Dec 13 '11 at 23:18
add comment

They go by a few different names.

C# calls them Multi-Key Generic Dictionaries.

As a generic term, they're known as multimaps or (less specifically) multi-key associative arrays. Now, "associative array" is essentially a hash-table, so they can also be known as a multihash.

But if you're sticking with C# (as mentioned in the question), Multi-Key Dictionaries.

Keep in mind, if you are going with C#, if you are sure that each of your keys is unique among all the keys you can try this solution:

dict.add("key1", object)
dict.add("key2", object)
share|improve this answer
    
That multi-key dictionary is the same as Java's MultiMap. Again, it's different than what I'm describing. Your proposed solution requires the multiple keys to be of the same type, a restriction I'd rather not impose on this data structure. You would be able to access item X through key "foo" or key 42. –  Daniel Dec 13 '11 at 23:29
    
@Daniel Fair enough. That solution is a quick hack at best. However, the names I mention are going to be the answer you seek. –  Richard Dec 13 '11 at 23:32
    
Is the collection I'm describing unusual, or are those names typical, yet ambiguous because they could refer to a map with compound keys, or multiple discrete keys? –  Daniel Dec 13 '11 at 23:33
    
@Daniel The names are ambiguous because they're not language specific names, but generic CS names. Check out this Wikipedia article for a clearer picture. –  Richard Dec 13 '11 at 23:36
add comment

To distinguish, and be very clear about it, the names I can think of are OrDualKeyDictionary and AndDualKeyDictionary which tells us how its supposed to work. But I agree its not very readable.

Another option is to name the former MultiKeyDictionary while the latter CompositeKeyDictionary. The "Multi" in former gives me a meaning that it can be queried by either key, while the latter, all the keys are required. Then again MultiKeyMap has a well established specific meaning which goes against my usage, but I find it a bit misplaced.

A third option is to keep it simple: AnyKeyDictionary and AllKeyDictionary.

Lastly, how about going by sql nomenclature? AlternateKeyDictionary and CompoundKeyDictionary?

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.