Dart bounded LRU cache with recency eviction

Dart
import 'dart:collection';

class LruCache<K, V> {
  LruCache(this.capacity) : assert(capacity > 0);
  final int capacity;
  final _map = <K, V>{};

  V? get(K key) {
    if (!_map.containsKey(key)) return null;
    final value = _map.remove(key) as V;
    _map[key] = value; // reinsert to mark as newest
    return value;
  }

  void put(K key, V value) {
    _map.remove(key);
    _map[key] = value;
    if (_map.length > capacity) _map.remove(_map.keys.first);
  }
}