-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLRU.java
More file actions
37 lines (29 loc) · 770 Bytes
/
LRU.java
File metadata and controls
37 lines (29 loc) · 770 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
33
34
35
36
37
package algorithm.note;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author: mayuan
* @desc: LRU算法的实现
* @date: 2018/09/14
*/
public class LRU<K> {
private static final float loadFactor = 0.75F;
private final int capacity;
private LinkedHashMap<K, K> map;
public LRU(int size) {
this.capacity = size;
int cap = (int) Math.ceil(size / loadFactor) + 1;
map = new LinkedHashMap<K, K>(cap, loadFactor, true) {
@Override
protected boolean removeEldestEntry(Map.Entry entry) {
return size() > capacity;
}
};
}
public K get(K key) {
return map.get(key);
}
public void put(K key) {
map.put(key, key);
}
}