lru cache java

时间:2024-10-19 09:04:50

http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html

acm之家的讲解是在是好,丰富

import java.util.LinkedHashMap;
public class LRUCache {
private int capacity;
private LinkedHashMap<Integer,Integer> map=null; public LRUCache(int capacity) {
this.capacity=capacity;
map=new LinkedHashMap<Integer,Integer>(16,0.75f,true){
protected boolean removeEldestEntry(java.util.Map.Entry<Integer,Integer> eldest) { return size()>LRUCache.this.capacity; } };
} public int get(int key) {
if(map.get(key)==null)
{
return -1;
}
return map.get(key); } public void set(int key, int value) {
map.put(key,value); }
}