Skip to content

Instantly share code, notes, and snippets.

@pwxcoo
Last active June 18, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwxcoo/239f05489e5689f7c15a9db06727cdde to your computer and use it in GitHub Desktop.
Save pwxcoo/239f05489e5689f7c15a9db06727cdde to your computer and use it in GitHub Desktop.
LRU implemented with LinkedHashMap in Java
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private int cacheSize;
public LRUCache(int cacheSize) {
super((int) Math.ceil(cacheSize / 0.75) + 1, 0.75f, true);
cacheSize = cacheSize;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > cacheSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment