Dart API Referencedart:coreimplLinkedHashMapImplementation<K, V>

LinkedHashMapImplementation<K, 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.

class LinkedHashMapImplementation<K, V>
    implements LinkedHashMap<K, V> {
  DoubleLinkedQueue<KeyValuePair<K, V>> _list;
  HashMap<K, DoubleLinkedQueueEntry<KeyValuePair<K, V>>> _map;

  LinkedHashMapImplementation() {
    _map = new HashMap<K, DoubleLinkedQueueEntry<KeyValuePair<K, V>>>();
    _list = new DoubleLinkedQueue<KeyValuePair<K, V>>();
  }

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

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

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

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

  V putIfAbsent(K key, V ifAbsent()) {
    V value = this[key];
    if ((this[key] === null) && !(containsKey(key))) {
      value = ifAbsent();
      this[key] = value;
    }
    return value;
  }

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


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

  void forEach(void f(K key, V value)) {
    _list.forEach(void _(KeyValuePair<K, V> entry) {
      f(entry.key, entry.value);
    });
  }

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

  bool containsValue(V value) {
    return _list.some(bool _(KeyValuePair<K, V> entry) {
      return (entry.value == value);
    });
  }

  int get length {
    return _map.length;
  }

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

  void clear() {
    _map.clear();
    _list.clear();
  }

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

Implements

LinkedHashMap<K, V>

Constructors

new LinkedHashMapImplementation() #

LinkedHashMapImplementation() {
  _map = new HashMap<K, DoubleLinkedQueueEntry<KeyValuePair<K, V>>>();
  _list = new DoubleLinkedQueue<KeyValuePair<K, V>>();
}

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

Properties

final int length #

The number of {key, value} pairs in the map.

docs inherited from Map<K, V>
int get length {
  return _map.length;
}

Operators

V operator [](K key) #

Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.

docs inherited from Map<K, V>
V operator [](K key) {
  DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map[key];
  if (entry === null) return null;
  return entry.element.value;
}

void operator []=(K key, V value) #

Associates the key with the given value.

docs inherited from Map<K, V>
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();
  }
}

Methods

void clear() #

Removes all pairs from the map.

docs inherited from Map<K, V>
void clear() {
  _map.clear();
  _list.clear();
}

bool containsKey(K key) #

Returns whether this map contains the given key.

docs inherited from Map<K, V>
bool containsKey(K key) {
  return _map.containsKey(key);
}

bool containsValue(V value) #

Returns whether this map contains the given value.

docs inherited from Map<K, V>
bool containsValue(V value) {
  return _list.some(bool _(KeyValuePair<K, V> entry) {
    return (entry.value == value);
  });
}

void forEach(void f(K key, V value)) #

Applies f to each {key, value} pair of the map.

docs inherited from Map<K, V>
void forEach(void f(K key, V value)) {
  _list.forEach(void _(KeyValuePair<K, V> entry) {
    f(entry.key, entry.value);
  });
}

Collection<K> getKeys() #

Returns a collection containing all the keys in the map.

docs inherited from Map<K, V>
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;
}

Collection<V> getValues() #

Returns a collection containing all the values in the map.

docs inherited from Map<K, V>
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;
}

bool isEmpty() #

Returns true if there is no {key, value} pair in the map.

docs inherited from Map<K, V>
bool isEmpty() {
  return length == 0;
}

V putIfAbsent(K key, V ifAbsent()) #

If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.

docs inherited from Map<K, V>
V putIfAbsent(K key, V ifAbsent()) {
  V value = this[key];
  if ((this[key] === null) && !(containsKey(key))) {
    value = ifAbsent();
    this[key] = value;
  }
  return value;
}

V remove(K key) #

Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.

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

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
  return Maps.mapToString(this);
}