Last active
June 18, 2019 01:17
-
-
Save pwxcoo/239f05489e5689f7c15a9db06727cdde to your computer and use it in GitHub Desktop.
LRU implemented with LinkedHashMap in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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