LinkedHashMap
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
LinkedHashMap is a HashMap that remembers the order of insertion of its keys. And with the flag accessOrder = true —
the order of the last access: each get moves the key to the end.
An LRU cache is built on this (Least Recently Used — "throw out what hasn't been used for the longest time"):
class LruCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
LruCache(int capacity) {
super(16, 0.75f, true); // true — the access order
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity; // called after put: true — remove the eldest
}
}
This is how loaded textures in games, database query results and pages in a browser are cached.
Task. Finish LruCache and carry out the commands: put key value, get key (the value or -),
show (the keys from the least recently used to the most recent, separated by spaces). The capacity is the first number of the input.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.