SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.LinkedHashMap; | |
| 2 | import java.util.Map.Entry; | |
| 3 | ||
| 4 | public class LRUCache extends LinkedHashMap<Integer, Integer> {
| |
| 5 | ||
| 6 | private int capacity; | |
| 7 | ||
| 8 | public LRUCache(int capacity) {
| |
| 9 | super(capacity+1, 1.0f, true); // for access order | |
| 10 | this.capacity = capacity; | |
| 11 | } | |
| 12 | ||
| 13 | public int get(int key) {
| |
| 14 | if(super.get(key) == null) | |
| 15 | return -1; | |
| 16 | else | |
| 17 | return ((int) super.get(key)); | |
| 18 | } | |
| 19 | ||
| 20 | public void set(int key, int value) {
| |
| 21 | super.put(key, value); | |
| 22 | } | |
| 23 | ||
| 24 | protected boolean removeEldestEntry(Entry entry) {
| |
| 25 | return (size() > this.capacity); | |
| 26 | } | |
| 27 | } |