• Advertisement
Sign in to follow this  

[.net] What collection would be best for this?

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently working on a little project in which I need to be able to quickly retrieve objects, where the key is NOT unique. Right now, I am using a List<Item> for storing all the objects, but this isn't very smartypants in my opinion, as the list of items can grow quite large. However, I want to be able to quickly retrieve items from the list. Preferably I want the list to be sorted and when searching for a key, just get the first position of the key(and then be able to determine which object to retrieve from the list). Is there a collection for this that suits my needs? I initially wanted to go with SortedList, but the keys must be uniue. Any ideas on what I could use? Toolmaker

Share this post


Link to post
Share on other sites
Advertisement
Use a Map<keyType, List<itemType>>? The keys are unique to a collection of items :)

(E: forgot how tags work here :P)

(E2: I don't know about SortedList but the same trick should work for using that too.)

Share this post


Link to post
Share on other sites
I believe HybridDictionary will give you the same problem. Also, I'm not sure what the Map class to which Bob Janova refers is, but a Dictionary<TKey, List<TValue>> should do the job for you. If you want to simplify the process of adding/removing entries, then you can just wrap the object in your own class (and use an indexer perhaps).

Share this post


Link to post
Share on other sites
Quote:
Original post by Fingon
I believe HybridDictionary will give you the same problem. Also, I'm not sure what the Map class to which Bob Janova refers is, but a Dictionary<TKey, List<TValue>> should do the job for you. If you want to simplify the process of adding/removing entries, then you can just wrap the object in your own class (and use an indexer perhaps).


Actually, I discovered this while I was sitting in the train today. I tried to add a 2nd object with the same key, and I got an exception.

I think I'm going to wrap this in another class.

Thanks for the help! ++rate.

Toolmaker

Share this post


Link to post
Share on other sites
Glad to help... Indeed, it is probably wise to wrap it in another class, as then you can make certain optimisations (such as using TryGetValue) or implement an enumerator, besides from the simpler syntax of course. Be aware of one disadvantage, which is slower add/remove times (especially under certain circumstances) compared to using a single list.

On a side note, (if you're using .NET 3.5) you may want to take a look at the System.Linq.Lookup<TKey, TElement> class, along with the ToLookup extension method. Although you can't directly use Lookup as a modifiable collection, it's useful when you want to transform other types of collections/lists into a lookup table (multi-key collection). Using it together with an ordinary List<T> is in fact the better option *if* you tend to be adding/removing items more often than you retrieve them.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
You could always use the MultiDictionary from Power Collections.


And we have a winner! Rate++ for you too. And, more importantly, I found the exact type I need: OrderedBag. Because what I'm going to implement is in fact an ItemBag, so the name is rather helpful.

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement