-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
68 lines (49 loc) · 1.61 KB
/
Copy pathLRUCache.java
File metadata and controls
68 lines (49 loc) · 1.61 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache {
private final int capacity;
private int hits = 0;
private int misses = 0;
private final LinkedHashMap<Integer, Integer> cache;
public LRUCache(int capacity) {
this.capacity = capacity;
this.cache = new LinkedHashMap<>(capacity, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
};
}
public int get(int key) {
if (cache.containsKey(key)) {
hits++;
return cache.get(key);
}
misses++;
return -1;
}
public void put(int key, int value) {
cache.put(key, value);
}
public void displayCache() {
System.out.println("Cache: " + cache);
}
public void displayStats() {
System.out.println("Hits: " + hits);
System.out.println("Misses: " + misses);
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
cache.displayCache();
System.out.println("Get 1: " + cache.get(1)); // Hit
cache.put(4, 40); // Evicts key 2
cache.displayCache();
System.out.println("Get 2: " + cache.get(2)); // Miss
System.out.println("Get 3: " + cache.get(3)); // Hit
System.out.println("Get 4: " + cache.get(4)); // Hit
cache.displayStats();
}
}