Actually the LinkedHashMap does exactly what you want. You need to override the removeEldestEntry
method.
Example for a queue with max 10 elements:
queue = new LinkedHashMap<Integer, String>()
{
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest)
{
return this.size() > 10;
}
};
If the "removeEldestEntry" returns true, the eldest entry is removed from the map.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…