Friday, September 11, 2009

Set a limit on the size of a LinkedHashMap

LinkedHashMap allows you to override the removeEldestEntry function so that whenever a put is executed, you can specify whether to remove the oldest entry or not, thus allowing implementation of an LRU.

LogMessages = new LinkedHashMap<Long,String>() {
protected boolean removeEldestEntry(Map.Entry eldest) {
if(this.size()>10000)
return true;
else
return false;
}
};

Labels

Blog Archive

Contributors