A neat trick in Java for building an LRU cache is to override the
Do you know any such tricks in other languages?
removeEldestEntry from a LinkedHashMap such as:public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > this.capacity;
}
}commit bca122c