package com.chan;
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUcache {
static class LRUCache<K,V> extends LinkedHashMap<K,V>{
private static final int MAX_ENTRIES = 3;
protected boolean removeEldestEntry(Map.Entry eldest){
return size()>MAX_ENTRIES;
}
LRUCache(){
super(MAX_ENTRIES,0.75f,true);
}
}
public static void main(String[] args){
LRUCache<Integer,String> cache= new LRUCache<>();
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
cache.get(1);
cache.put(4, "d");
System.out.println(cache.keySet());
}
}
输出结果
[3, 1, 4]