Dictionary with duplicate Key [duplicate]
Asked Answered
B

3

10

Possible Duplicate:
Is there an alternative to Dictionary/SortedList that allows duplicates?

I am looking for a Dictionary sort of class which can have duplicate Keys.

I search about it, and found LookUp class can use to store duplicate keys, but It has no default constructor, So we can't initialize it without any other object to LookUp.

But I have no any such object initially from which I can initialize a LookUp object.

So, My question is, Is there any class in .Net framework 3.5, which behave like Dictionary but allow me to have duplicate keys like LookUp?

Brion answered 1/11, 2012 at 20:8 Comment(8)
May I enquire why you would like duplicated key? You could store an array as your value instead.Witting
@Marc-AndréJutras, Yes, I want to store user action with its affected content so, I can perform a merge like operation on those affected object. I can do this one by one, but it is time consuming so I am just storing those, and then perform in a single, so only that action take time not all.Brion
How about using Dictionary<K,List<V>> instead of Dictinary<K,V>, similar to Lookup?Cesarcesare
@YograjGupta: Then why is it a dictionary? A dictionary is made to be fast at searching a key. If you don't plan to do that, there is no point and you could just use a List<Tuple<MyAction, MyData>>Witting
@LB, My current implementation is with Dictionary<NetworkAction, Dictionary<NetworkContent, object>> _actionMapping but I can have a single action multiple time.Brion
@YograjGupta you said you're looking for something similar to lookup. It is almost the same.Cesarcesare
@LB, Yes, But it only will have a single action, as key. but an action can be repeat multiple times. I can create a class for that which can perform all task which I want, but I am looking for if is there in .Net Framework.Brion
Everyone here is saying "no", which is true in general, but if you are storing strings then .net has NameValueCollection. MSDN: this class stores multiple string values under a single keySoot
K
12

A Dictionary, by definition, will never be able to have multiple keys with the same value. (If you looked up a key whatever would you return?) Even a Lookup, which you refer to, doesn't allow it. What you can do is have each key refer to multiple values (logically, not technically). This is done by having a dictionary in which the value is a data structure of some sort (for example, a List) that contains all of the values that correspond to that particular key.

Knipe answered 1/11, 2012 at 20:10 Comment(4)
This would be something like Ninject's MultiMapSir
I use Dictionary<int, List<string>> (or similar) all the time.Featherbedding
@Sir Yeah, that's just a wrapper around a Dictionary<K, List<V>>; it doesn't actually do all that much for you, but it does a bit. Usually I find that working with a Dictionary<T, List<V>> directly is simple enough, as Bobson mentions.Knipe
@Servy, Thanks for your help, I know Dictionary and LookUp for what and why we use them, but I want to get sequential lookup.Brion
C
20

You could create a list of key value pairs.

List<KeyValuePair<string,int>>
Comprise answered 1/11, 2012 at 20:10 Comment(5)
But then it's not logically a dictionary, it's just a list of pairs. (Which may very well be what he needs/wants, I'm just being technical.)Knipe
But then the key retrieval would suffer. A dictionary is made to be fast in key research.Witting
True, but I'm not sure how you could have something that allows duplicate keys and keeps the advantages of a dictionary. What it seems like is he should use a dictionary with a list of values so that when a key is duplicated, the second value is added to the list and both are retrieved when looking up the key. [Edit: What Servy said in his answer, upping his answer]Comprise
True, but I'm not sure how you could have something that allows duplicate keys and keeps the advantages of a dictionary. You can't (technically). It's impossible, by definition, as I said in my answer.Knipe
@Knipe Exactly. I didn't see your answer right away, but it's the one I would go with too. I was trying to answer his question directly, but you provided a better solution.Comprise
K
12

A Dictionary, by definition, will never be able to have multiple keys with the same value. (If you looked up a key whatever would you return?) Even a Lookup, which you refer to, doesn't allow it. What you can do is have each key refer to multiple values (logically, not technically). This is done by having a dictionary in which the value is a data structure of some sort (for example, a List) that contains all of the values that correspond to that particular key.

Knipe answered 1/11, 2012 at 20:10 Comment(4)
This would be something like Ninject's MultiMapSir
I use Dictionary<int, List<string>> (or similar) all the time.Featherbedding
@Sir Yeah, that's just a wrapper around a Dictionary<K, List<V>>; it doesn't actually do all that much for you, but it does a bit. Usually I find that working with a Dictionary<T, List<V>> directly is simple enough, as Bobson mentions.Knipe
@Servy, Thanks for your help, I know Dictionary and LookUp for what and why we use them, but I want to get sequential lookup.Brion
H
3

You can compose a type yourself by using a dictionary of lists, Dictionary<TKey, List<TValue>>

You can create a class inheriting from that class and add suitable add-methods etc that handles creating a new list for the first item on a given key.

Hydrate answered 1/11, 2012 at 20:14 Comment(1)
Y'know - I use this structure all teh time, but I never thought of making a class to handle it. I'd done a few extension methods, but not a class. Good idea.Featherbedding

© 2022 - 2024 — McMap. All rights reserved.