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?

Community
  • 1
  • 1
Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
  • 7
    May I enquire why you would like duplicated key? You could store an array as your value instead. – LightStriker Nov 01 '12 at 20:09
  • @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. – Yograj Gupta Nov 01 '12 at 20:13
  • 1
    How about using `Dictionary>` instead of `Dictinary`, similar to Lookup? – L.B Nov 01 '12 at 20:14
  • 1
    @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> – LightStriker Nov 01 '12 at 20:15
  • @LB, My current implementation is with `Dictionary> _actionMapping` but I can have a single action multiple time. – Yograj Gupta Nov 01 '12 at 20:16
  • @YograjGupta you said you're looking for something similar to lookup. It is almost the same. – L.B Nov 01 '12 at 20:17
  • @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. – Yograj Gupta Nov 01 '12 at 20:19
  • Everyone here is saying "no", which is true in general, but if you are storing strings then .net has [NameValueCollection](https://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx). MSDN: `this class stores multiple string values under a single key` – AnorZaken Apr 03 '16 at 16:21

3 Answers3

20

You could create a list of key value pairs.

List<KeyValuePair<string,int>>
madeFromCode
  • 721
  • 7
  • 15
  • 2
    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.) – Servy Nov 01 '12 at 20:11
  • But then the key retrieval would suffer. A dictionary is made to be fast in key research. – LightStriker Nov 01 '12 at 20:12
  • 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] – madeFromCode Nov 01 '12 at 20:17
  • `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. – Servy Nov 01 '12 at 20:19
  • @Servy 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. – madeFromCode Nov 01 '12 at 20:25
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.

TechSavvySam
  • 1,382
  • 16
  • 28
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    This would be something like Ninject's [`MultiMap`](https://github.com/ninject/ninject/blob/d663e013632780ca3d47818b7ba372a103a4ac9d/src/Ninject/Infrastructure/Multimap.cs) – khellang Nov 01 '12 at 20:13
  • 1
    I use `Dictionary>` (or similar) all the time. – Bobson Nov 01 '12 at 20:14
  • 1
    @khellang Yeah, that's just a wrapper around a `Dictionary>`; it doesn't actually do all that much for you, but it does a bit. Usually I find that working with a `Dictionary>` directly is simple enough, as Bobson mentions. – Servy Nov 01 '12 at 20:16
  • @Servy, Thanks for your help, I know Dictionary and LookUp for what and why we use them, but I want to get sequential lookup. – Yograj Gupta Nov 01 '12 at 20:38
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.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • 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. – Bobson Nov 01 '12 at 20:15