Collection of key-value pairs.
TRSDictionary represents a generic collection of key-value pairs.
This class provides a mapping from a collection of keys to a collection of values. When you create a TRSDictionary object, you can specify various combinations of initial capacity, equality operation, and initial content.
You can add a key that is associated with a corresponding value with the Add or AddOrSetValue methods. You can remove entries with Remove or Clear, which removes all key-value pairs. Adding or removing a key-value pair and looking up a key are efficient, close to O(1), because keys are hashed. A key must not be nil (though a value may be nil) and there must be an equality comparison operation for keys.
You can test for the presence or keys and values with the TryGetValue, ContainsKey and ContainsValue methods.
The Items property lists all Count dictionary entries. You can also set and get values by indexing the Items property. Setting the value this way overwrites any existing value.
Note |
|
---|---|
The TRSDictionary<TKey,TValue> class was created to address problems in the TDictionary class that prevented subclassing from the class. The TRSHashTable<TKey,TValue> needed some changes (as of XE4) in order to properly mimic the older TGHashTable class from RiverSoftAVG:
|
Namespace: RSGenerics.Collections
TPersistent |
Delphi |
type |
Type Parameters
TKey
TValue
|
|
Setting the value this way overwrites the value for an existing key, but does not raise an exception. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Add key-value pair even when key already exists. AddOrSetValue adds a key-value pair to a dictionary even if the key already exists. The key cannot be nil, but the value can. This method checks to see if the key exists in the dictionary, and if it does, it is equivalent to Items[key] := value;. Otherwise it is equivalent to Add(key, value);. An OnKeyNotify event and an OnValueNotify event occur indicating an entry was added to the dictionary. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copies the source dictionary to the current dictionary. The current dictionary is cleared first. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Clear all data. Clear removes all keys and values from the dictionary. The Count property is set to 0. The capacity is also set to 0. This operation requires O(n) time, where n is Count, the number of dictionary entries.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Test if key in dictionary. ContainsKey returns true if the given key is in the dictionary and false otherwise. This is an O(1) operation. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check if value in dictionary. ContainsValue returns true if the given value is in the dictionary and false otherwise. This is an O(n) operation, where n is the number of entries in the dictionary. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Adds key/value pair to dictionary at the given index |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns the container's enumerator. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Removes key/value pair from dictionary |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Overwrites value in dictionary at the given index |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns the TPair<TKey,TValue> pair with the specified Key and removes the returned pair from a dictionary. If the dictionary does not contain the specified Key, the returned pair contains a default TValue. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns an key-value pair enumerator for the dictionary. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Triggers an OnKeyNotify event with the given parameters. Call KeyNotify to trigger an OnKeyNotify event with the given parameters. The meaning of the parameters is given in the following table: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Key |
The key that is added, removed or extracted. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Action |
The action that the key undergoes, which is of type TCollectionNotification. |
Remove key-value pair.
Remove removes the given key and its associated value from the dictionary. No exception is thrown if the key is not in the dictionary. This is an O(1) operation.
An OnKeyNotify event and an OnValueNotify event occur indicating an entry was removed from the dictionary.
Returns the content of the dictionary as an array.
(Overrides RSGenerics.Collections.TRSEnumerable<T>.ToArray.)
Converts the enumerable container's items into an equivalent TArray of items. (Inherited from RSGenerics.Collections.TRSEnumerable<T>.)
Reduce capacity to current number of entries.
TrimExcess changes the capacity to the number of dictionary entries, held in Count.
This method rehashes the internal hash table to save space. This is only useful after a lot of items have been deleted from the dictionary.
Try to get value for key.
TryGetValue returns true if the given key is in the dictionary and provides its value in Value. Otherwise, it returns false and Value is set to the default value type of TValue. No exception is raised if the key is not in the dictionary. This is an O(1) operation.
ValueNotify(TValue,TCollectionNotification)
Triggers an OnValueNotify event with the given parameters.
Call ValueNotify to trigger an OnValueNotify event with the given parameters.
The meaning of the parameters is given in the following table:
Value
The Value that is added, removed or extracted.
Action
The action that the Value undergoes, which is of type TCollectionNotification.
Reference•TGHashtable |