A collection of keys / values ​​that maintain order

I need a delphi key / value collection that will allow me to iterate over the collection in the same order in which the key / value pairs were inserted / added.

TList<T> guarantees order, but TDictionary<T1, T2> does not.

I guess I could always define TList<TPair<Key, Value>> , but it would be much harder to work with.

Is there a built-in collection type that suits my requirements or could wrap TList<TPair<Key, Value>> best option? Or perhaps it would be better to have a TList<Key> and a TDictionary<Key, Value> and TDictionary<Key, Value> over the list.

+7
source share
2 answers

If your key type is string and your value type is some descendant of TObject , use a TStringList . Store your values ​​in the property of the Objects array.

 SL.AddObject('foo', obj1); SL.Add('bar'); i := SL.IndexOf('bar'); SL.Objects[i] := obj2; 

Set the OwnsObjects property if you need to.

+3
source

The DeHL collection library contains many classes that look like an ordered dictionary. Ordered use trees (which are of order) instead of hash cards that are unordered.

I believe that TSortedDistinctMultiMap may be what you need if you want to apply uniqueness, and if you do not want to use the uniqueness of Key uniqueness, then there are other options (without Distinct in the class name) that will be close to what you need .

+3
source

All Articles