LinkedHashMapImplementation<K extends Hashable, V> Class
A LinkedHashMap is a hash map that preserves the insertion order when iterating over the keys or the values. Updating the value of a key does not change the order.
Implements
Constructors
Code new LinkedHashMapImplementation() #
LinkedHashMapImplementation() { _map = new HashMap<K, DoubleLinkedQueueEntry<KeyValuePair<K, V>>>(); _list = new DoubleLinkedQueue<KeyValuePair<K, V>>(); }
Code factory LinkedHashMapImplementation.from(Map<K, V> other) #
factory LinkedHashMapImplementation.from(Map<K, V> other) { Map<K, V> result = new LinkedHashMapImplementation<K, V>(); other.forEach((K key, V value) { result[key] = value; }); return result; }
Methods
Code void clear() #
void clear() { _map.clear(); _list.clear(); }
Code bool containsValue(V value) #
bool containsValue(V value) { return _list.some(bool _(KeyValuePair<K, V> entry) { return (entry.value == value); }); }
Code void forEach(void f(K key, V value)) #
void forEach(void f(K key, V value)) { _list.forEach(void _(KeyValuePair<K, V> entry) { f(entry.key, entry.value); }); }
Code Collection<K> getKeys() #
Collection<K> getKeys() { List<K> list = new List<K>(length); int index = 0; _list.forEach(void _(KeyValuePair<K, V> entry) { list[index++] = entry.key; }); assert(index == length); return list; }
Code Collection<V> getValues() #
Collection<V> getValues() { List<V> list = new List<V>(length); int index = 0; _list.forEach(void _(KeyValuePair<K, V> entry) { list[index++] = entry.value; }); assert(index == length); return list; }
Code void operator []=(K key, V value) #
void operator []=(K key, V value) { if (_map.containsKey(key)) { _map[key].element.value = value; } else { _list.addLast(new KeyValuePair<K, V>(key, value)); _map[key] = _list.lastEntry(); } }
Code V operator [](K key) #
V operator [](K key) { DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map[key]; if (entry === null) return null; return entry.element.value; }
Code V putIfAbsent(K key, V ifAbsent()) #
V putIfAbsent(K key, V ifAbsent()) { V value = this[key]; if ((this[key] === null) && !(containsKey(key))) { value = ifAbsent(); this[key] = value; } return value; }