-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLRUCache.java
More file actions
30 lines (24 loc) · 805 Bytes
/
LRUCache.java
File metadata and controls
30 lines (24 loc) · 805 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
package learnCollections;
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private int capacity;
LRUCache (int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
// invoke when put or putAll is called
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}
public static void main(String[] args) {
LRUCache<String, Integer> studentMap = new LRUCache<>(3);
studentMap.put("Bob", 99);
studentMap.put("Alice", 89);
studentMap.put("Ram", 91);
studentMap.get("Bob");
studentMap.put("Rahul", 89);
System.out.println(studentMap); // {Ram=91, Bob=99, Rahul=89}
}
}