-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
32 lines (26 loc) · 800 Bytes
/
LRUCache.java
File metadata and controls
32 lines (26 loc) · 800 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
32
package org.sean.array;
import java.util.LinkedHashMap;
import java.util.Map;
/** * 146. LRU Cache */
public class LRUCache {
private final LinkedHashMap<Integer, Integer> map;
private final int size;
public LRUCache(int capacity) {
size = capacity;
// enable accessOrder
map =
new LinkedHashMap<Integer, Integer>(size, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > size;
}
};
}
public int get(int key) {
Integer val = map.get(key);
return val == null ? -1 : val;
}
public void put(int key, int value) {
map.put(key, value);
}
}