一、使用LinkedHashMap实现
class LRUCache {
Map<Integer, Integer> cache=null;
public LRUCache(int capacity) {
cache = new LinkedHashMap<>(capacity, 0.75f, true){
@Override
public boolean removeEldestEntry(Map.Entry eldest){
if(this.size()>capacity){
return true;
}
return false;
}
};
}
public int get(int key) {
Integer v = this.cache.get(key);
return v==null?-1:v.intValue();
}
public void put(int key, int value) {
this.cache.put(key,value);
}
}
二、使用HashMap 和DoubleList 实现
class LRUCache {
int capacity;
DoubleList cache;
Ma