Delphi Collections Key Features
The kind of library found in other, modern languages
- More types - TList is only a list, so is painful to use as a lookup table and requires sequential trawling. Other languages use maps or dictionaries that quickly find data in in-memory, lookup tables via an identifying or key value. This is achieved with collections implementing IMap, though IIntegerMap and IStringMap are more commonly used for lookup by integer or string. As well as lists as IList, developers using Smalltalk or C++ will be familiar with sets and bags (multisets) as ISet and IBag.
- Garbage collection - unreferenced objects are automatically destroyed, thus releasing developers from considering every possible scenario where an object may be dropped. Collections accept ICollectable references, thus using interface reference counting to track objects. Objects that do not implement ICollectable can be adapted using classes such as TObjectWrapper. Collections themselves are usually referenced using interface references and, thus, are automatically destroyed when they are out of scope.
- Search by item value, not object reference - two, different items can be treated as equal according to its native value (see What are natural collections, items and keys? in downloadable help file) or using user-written comparators. This is useful for sets or maps.
- Function versions for single items, collections and array - operations such as adding, detecting or removing can use multiple-item collections and arrays as well as single items.
- Filter functions - particular items can be extracted according to user-written filter classes or functions. This can be achieved by the Find(Filter), FindAll and GetIterator(Filter) functions, amongst others.
- List sort order is maintained - once a list is set as sorted, Add(Item) functions inserts the item into the correct place.
- Cloning - Clone creates a shallow copy of a collection.