201021-LRU算法急速实现版

借助LinkedHashMap飞速实现一个LRU算法的缓存

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
import java.util.LinkedHashMap;
import java.util.Map;

/**
* 基于LinkedHashMap实现的LRU算法
* Created by @author yihui in 17:36 20/10/19.
*/
public class LruCache<K, V> extends LinkedHashMap<K, V> {
private int size;

public LruCache(int size) {
super(size, 0.75f, true);
this.size = size;
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
// 当元素个数,超过指定的大小时,淘汰最老的数据
return size() > size;

}

public static void main(String[] args) {
LruCache<String, Integer> cache = new LruCache<>(4);
for (int i = 0; i < 6; i++) {
cache.put("key_" + i, i);
System.out.println(cache);
}

System.out.println(cache.size);
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×