使用LinkedHashMap实现的LRU算法
2014-04-14 20:29
316 查看
使用LinkedHashMap实现的LRU算法,算是一种偷懒吧.
public class LRUCache < K, V > extends java.util.LinkedHashMap < K, V > {
private static final long serialVersionUID = 1L;
private int capacity;
public LRUCache(int capacity) {
super(capacity+1, 1.0f, true); // Pass 'true' for accessOrder.
this.capacity = capacity;
}
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> entry) {
return (size() > this.capacity);
}
public V getValue(K key) {
if(this.keySet().contains(key)){
V value = this.get(key);
this.put(key, value);
return value;
}else{
return null;
}
}
public void setValue(K key, V value) {
this.put(key, value);
}
}
public class LRUCache < K, V > extends java.util.LinkedHashMap < K, V > {
private static final long serialVersionUID = 1L;
private int capacity;
public LRUCache(int capacity) {
super(capacity+1, 1.0f, true); // Pass 'true' for accessOrder.
this.capacity = capacity;
}
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> entry) {
return (size() > this.capacity);
}
public V getValue(K key) {
if(this.keySet().contains(key)){
V value = this.get(key);
this.put(key, value);
return value;
}else{
return null;
}
}
public void setValue(K key, V value) {
this.put(key, value);
}
}
相关文章推荐
- Ubuntu 搭建 NFS服务提供给 SC2440或SC6410开发板使用【要点整理】
- windows2003搭建SVN服务器
- 在Ubuntu下用Eclipse开发ARM多线程程序
- win7下简单搭建Qt开发环境(不需安装vs)
- Ubuntu10.10下QTEmbedded-4.5.2在DM6446环境下的搭建
- eclipse + cdt + gdb 进行arm linux 交叉调试
- 我的博客搬家啦~
- [LeetCode] String to Integer (atoi)
- hdu 1231 最大连续子序列
- 总结之:CentOS 6.5 MySQL数据库的基础以及深入详解
- Arm 系统中常用的段分类说明
- lp 命令
- LaunchActivity\StartActivity详细时序图
- Linux下必看的60个命令
- ubuntu 10.04 下 samba 服务的配置
- WebService和Socket的联系与区别
- Java :List
- AngularJS $http服务
- 使用ant将flash air游戏编译和打包
- NYOJ 7 街区最短路径问题