I'm trying to see if modifying a custom cache implementation to use generics in the LinkedHashMap
instead of LinkedHashMap<Object, Object>
. I presume the <Object, Object>
is suitable, but what is the best practice? Does the use of generics make sense?
public class Cache extends LinkedHashMap<Object, Object>{
private static final long serialVersionUID = -4297992703249995219L;
private final int cacheSize;
public Cache(int size){
super(size + 1, .75f, true);
this.cacheSize=size;
}
protected boolean removeEldestEntry(Map.Entry<Object, Object> eldest) {
return size() > cacheSize;
}
}
public class Main {
public static void main(String[] args) {
Cache cache = new Cache(2);
cache.put(1, "one");
cache.put(2, "two");
for(Entry<Object, Object> entry : cache.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}