import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;public class A{
public static void main(String[] args){
try{
Random r=new Random(System.currentTimeMillis());
LRUMap<Integer,Product> cache=new LRUMap<Integer, Product>();
int cacheSize=3;
for(int i=0;i<10;i++){
int key=r.nextInt(5);
Product p;
if(!cache.containsKey(key)){
p=new Product(key);
if(cache.size()>=cacheSize){
}
cache.put(key, p);
}else{
p=cache.getP(key);
}
System.out.print(key+":");
System.out.println();
System.out.println(cache);
}
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("Over!!");
}
}

public static class Product{
Integer key;
public Product(Integer key){
this.key=key;
}
public Integer getKey(){
return this.key;
}

public String toString(){
return this.key.toString();
}
}

public static class LRUMap<K,V> extends LinkedHashMap<K,V>{
private static final long serialVersionUID = 1L;      private static final int MAX_ENTRIES = 3;      protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return size() > MAX_ENTRIES;
     } public V getP(K key) {
V ret=super.get(key);
if(ret!=null){
this.remove(key);
this.put(key,ret);
}
return ret;
}
     
}
}