-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathques-11-lru-cache.java
More file actions
31 lines (26 loc) · 983 Bytes
/
Copy pathques-11-lru-cache.java
File metadata and controls
31 lines (26 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* QUESTION: LRUCache<K,V> — a fixed-capacity cache built on LinkedHashMap
* using access-order mode plus an overridden removeEldestEntry(), evicting
* the least-recently-used entry once capacity is exceeded. Used by
* pro-3-rbac-engine to cache resolved permission sets.
*
* Input: cache of capacity 2: put(1,"a"); put(2,"b"); get(1); put(3,"c")
* Output: key 2 gets evicted (least recently used), 1 and 3 remain
*/
import java.util.LinkedHashMap;
import java.util.Map;
class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
LRUCache(int capacity) {
super();
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
throw new UnsupportedOperationException("TODO");
}
}
// --- TEST ---
// LRUCache<Integer, String> cache = new LRUCache<>(2);
// cache.put(1, "a"); cache.put(2, "b"); cache.get(1); cache.put(3, "c");
// System.out.println(cache.keySet()); // expected [1, 3]