您的位置:首页 > 其它

146. LRU Cache

2016-05-04 15:22 211 查看
在写代码前看了两篇博客才大概理解了java中的hashmap和linkedhashmap

http://zhangshixi.iteye.com/blog/673789

http://zhangshixi.iteye.com/blog/672697

import java.util.HashMap;
import java.util.LinkedHashMap;
public class LRUCache extends LinkedHashMap<Integer,Integer>{
private int maxcapacity;
public LRUCache(int capacity) {
super(capacity,0.75f,true);
this.maxcapacity=capacity;
}

public int get(int key) {
Integer vaule=super.get(key);
if(vaule==null)return -1;
else return vaule;
}

public void set(int key, int value) {
super.put(key,value);
}
protected boolean removeEldestEntry(Map.Entry<Integer,Integer> eldest)
{
return size()>maxcapacity;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: