public class LRULinkedHashMap extends java.util.LinkedHashMap { public final int maxCapacity; public LRULinkedHashMap(int capacity){ this.maxCapacity=capacity; } @Override protected boolean removeEldestEntry(java.util.Map.Entry eldest) { // TODO Auto-generated method stub return size()>maxCapacity; } public int get(int key){ if(super.containsKey(key)){ Integer value=(Integer) super.remove(key); set(key,value); return value; } return -1; } public void set(int key,int value){ if(super.containsKey(key)){ super.remove(key); } super.put(key, value); } public static void main(String[] args) { LRULinkedHashMap main2=new LRULinkedHashMap(2); main2.set(2, 1); main2.set(1, 1); main2.set(2, 3); main2.set(4, 1); //System.out.println(main2.get(2)); /*Set<Map.Entry<Integer,Integer>> set=main2.entrySet(); Iterator<Map.Entry<Integer,Integer>> it=set.iterator(); while (it.hasNext()) { Map.Entry<java.lang.Integer, java.lang.Integer> entry = (Map.Entry<java.lang.Integer, java.lang.Integer>) it .next(); System.out.println(entry.getKey()+" "+entry.getValue()); }*/ System.out.println(main2.get(1)); System.out.println(main2.get(2)); }