Shrink LinkedHashMap in Java
The iterator will iterate from oldest to youngest for LinekdHashMap. You if you want to shrink the LinkedHashMap to a size you can use the following.
Map<K,V> lhm =
int desiredSize =
for(Iterator iter = lhm.keySet().iterator();iter.hasNext()) {
if(lhm.size() <= desiredSize) break;
iter.next(); //required else IllegalStateException since current=null
iter.remove();
}
This should take about 20 ns per entry removed.