Dictionary<K, V> Class

Maintains a mapping of keys to values. Unlike the standard Map<K, V>, a Dictionary<K, V> supports custom comparison logic for keys of object type (and for any other type). The user supplies a key comparison function to the constructor, that must meet the following criteria given 'lhs' and 'rhs' of type K:

  • If lhs is equal to rhs, returns 0
  • If lhs is less than rhs, returns a negative value
  • If lhs is greater than rhs, returns a positive value
  • If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
  • If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.

Modifying a key in a way that affects the comparison function will produce unpredictable results, the most likely of which is that keys will cease to map to the values with which they were initially inserted.



Name Description
constructor(compareKeys: OrderedComparator<K>, cloneKey: shallowClone<K> = shallowClone, cloneValue: shallowClone<V> = shallowClone): Dictionary Construct a new Dictionary<K, V>.  
[Symbol.iterator](): Iterator<DictionaryEntry<K, V>> Returns an iterator over the key-value pairs in the Dictionary suitable for use in for-of loops.  
clear(): void Removes all entries from this dictionary  
delete(key: K): boolean Deletes a value using its key.  
extractArrays(): { keys: K[], values: V[] } Extracts the contents of this dictionary as a pair of { keys, values } arrays, and empties this dictionary.  
extractPairs(): Array<{ key: K, value: V }> Extracts the contents of this dictionary as an array of { key, value } pairs, and empties this dictionary.  
findOrInsert(key: K, value: V): { inserted: boolean, value: V } Obtains the value associated with the specified key, or inserts it if the specified key does not yet exist.  
forEach(func: (key: K, value: V) => void): void Apply a function to each (key, value) pair in the dictionary, in sorted order.  
get(key: K): V | undefined Looks up a value by its key.  
has(key: K): boolean Determines if an entry exists for the specified key  
insert(key: K, value: V): boolean Attempts to insert a new entry into the dictionary.  
keys(): Iterable<K> Provides iteration over the keys in this Dictionary, in sorted order.  
lowerBound(key: K): { equal: boolean, index: number } Protected Computes the position at which the specified key should be inserted to maintain sorted order.  
set(key: K, value: V): void Sets the value associated with the specified key in the dictionary.  
values(): Iterable<V> Provides iteration over the values in this Dictionary, in sorted order by the corresponding keys.  


Name Type Description
_cloneKey Protected shallowClone<K>    
_cloneValue Protected shallowClone<V>    
_compareKeys Protected OrderedComparator<K>    
_keys Protected K[]    
_values Protected V[]    
size Accessor ReadOnly number The number of entries in the dictionary.  

Defined in

Last Updated: 02 July, 2021