Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered List of Keyvaluepairs?

Is there an collection in .net that allows the storing KeyValuePair<string, string> that keeps the order of inserting?
OrderedDictionary looked promising, but seems to be rather lacking.
Now I'm looking into IOrderedEnumerable>, but I can't seem to find any implementation except for ISortedDictionary, but that's not what I want. No sorting needs to be done, just the order of inserting is important.

Update
The reason I don't like OrderedDictionary is that it's not generic.

like image 309
Boris Callens Avatar asked Jun 18 '10 14:06

Boris Callens


3 Answers

Although I am late to the game, .NET Framework 4.5 provides new classes for you. See SortedList<TKey, TValue> or SortedDictionary<TKey, TValue>. If you are wondering which one you should use, the MSDN provides a few good reasons why you might choose one over the other.

The SortedList generic class is an array of key/value pairs with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

  • SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.
  • SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.
  • If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

Another difference between the SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> classes is that SortedList<TKey, TValue> supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values.

Both links have similar Remarks section (which is where the quote comes from). They also have more information for both classes. I'd recommend reading both sections if you are interested in using one of them.

like image 195
techvice Avatar answered Sep 23 '22 15:09

techvice


OrderedDictionary is what you want if you need both keyed and insertion-sequenced access to items ... it's really just a combination of a hash table and a list. It provides a means to access items in it either by insertion index or by key. It's the only collection in .NET that does this. Sadly, it is not generic.

If OrderedDictionary doesn't meet your needs solely because it is not generic - then you can use the version here that provides a generic equivalent. If there are other reasons why it doesn't work for you, update your post and we can look for a better option.

While you can certainly create your own List<KeyValuePair<string,string>> you will lose the option of searching by key efficiently. Now, you can certainly roll your own implementation of an ordered doctionary that combined list/dict together ... but the post I've linked to already does this.

like image 38
LBushkin Avatar answered Sep 23 '22 15:09

LBushkin


Just use List<KeyValuePair<T,T>>. They are stored in order of insertion. Every time you add to it, the newest one is added to the end of the list.

so

var list = new List<KeyValuePair<String,String>>();

list.Add(new KeyValuePair<String,String>("",""));

If you want to pull them out in order just use:

list.ForEach(x=>...);

or

foreach(var item in list){
...}
like image 40
kemiller2002 Avatar answered Sep 21 '22 15:09

kemiller2002