Alex Rivera | Logout

Key/Value Collection that maintains order

Asked 2011-10-31T15:37:55.850
9

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

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

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

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

Edit
Report

1 Answer

3

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

SL.AddObject('foo', obj1);

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

Set the OwnsObjects property if you need to.

answered 2011-10-31T16:55:13.993

Your Answer