Wednesday, 18 December 2019

LRU Implementation in Java

package kp;

import java.util.LinkedHashMap;

public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L;
private int size;

public LRUCache(int size) {
super(size, 0.75f, true);
this.size = size;
}

@Override
protected boolean removeEldestEntry(java.util.Map.Entry<K, V> paramEntry) {
return size() > size;
}

public static void main(String args[]) {

LRUCache<String, String> lruCache = new LRUCache<String, String>(7);

lruCache.put("2", "2");
lruCache.put("1", "1");
lruCache.put("3", "3");
lruCache.put("4", "4");
lruCache.put("5", "5");
lruCache.put("6", "6");
lruCache.put("7", "7");



//System.out.println("---" + lruCache.get("1"));
//System.out.println("---" + lruCache.get("2"));
//System.out.println("---" + lruCache.get("3"));

System.out.println(lruCache);
}

}

No comments:

Post a Comment