operator []= method

void operator []=(
  1. K key,
  2. V value
)

Associates value with key.

If key is already present, its value is updated and it becomes the most recently used entry. Otherwise, if the cache is full, the least recently used entry is evicted before the new entry is inserted.

Implementation

void operator []=(K key, V value) {
  if (_cache.containsKey(key)) {
    _cache.remove(key);
  } else if (_cache.length >= capacity) {
    // The cache is full: evict the least recently used entry (the first).
    _cache.remove(_cache.keys.first);
  }

  _cache[key] = value;
}