Domanda di colloquio di LinkedIn

Code a generic Java LRU cache in 35 minutes.

Risposta di colloquio

Anonimo

10 dic 2011

Basically extend LinkedHashMap. The point to emphasize is in calling the constructor with a loadfactor of 1 and setting the accesorder to true. Below is the implementation for it. public class LRUCache extends LinkedHashMap { /** * */ private static final long serialVersionUID = 1L; private int maxSize; public LRUCache(int size){ super(size+1,1,true); maxSize = size; } @Override protected boolean removeEldestEntry(Entry eldest){ return size() > maxSize; } }

1