Dart API Referencedart:coreimplLinkedHashMapImplementation<K, V>

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

LinkedHashMap<K, V>

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 containsKey(K key) #

bool containsKey(K key) {
  return _map.containsKey(key);
}

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 bool isEmpty() #

bool isEmpty() {
  return length == 0;
}

Code int get length() #

int get length() {
  return _map.length;
}

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;
}

Code V remove(K key) #

V remove(K key) {
  DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map.remove(key);
  if (entry === null) return null;
  entry.remove();
  return entry.element.value;
}

Code String toString() #

String toString() {
  return Maps.mapToString(this);
}