如何在FileOutputStream
,DataOutputStream
和writeObject()
的幫助下覆蓋removeEldestEntry
方法將最老的條目保存到文件中。碼。removeEldestEntry
這裏的例子:
import java.util.*;
public class level1 {
private static final int max_cache = 50;
private Map cache = new LinkedHashMap(max_cache, .75F, true) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > max_cache;
}
};
public level1() {
for (int i = 1; i < 52; i++) {
String string = String.valueOf(i);
cache.put(string, string);
System.out.println("\rCache size = " + cache.size() +
"\tRecent value = " + i + " \tLast value = " +
cache.get(string) + "\tValues in cache=" +
cache.values());
}
通用的代碼風格慣例將有MAX_CACHE寫在全部大寫,因爲它是一個靜態的最後的/不變。 – 2012-01-17 21:53:15