가장 오래된 항목을 파일로 저장하려면 removeEldestEntry 메서드를 재정의 할 수 있습니까? 또한 LinkedHashMap에서했던 것처럼 파일의 크기를 제한하는 방법도 있습니다.removeEldestEntry override
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());
}
내가 FileOutputStream에 사용하려고 : 여기에 코드입니다
private Map cache = new LinkedHashMap(max_cache, .75F, true) {
protected boolean removeEldestEntry(Map.Entry eldest) throws IOException {
boolean removed = super.removeEldestEntry(eldest);
if (removed) {
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(eldest.getValue());
oos.close();
}
return removed;
}
을하지만 오류
오류 (15,27) 얻고있다 : 위해서, removeEldestEntry (java.util.Map에게 .Entry)는 java.util.LinkedHashMap에서 removeEldestEntry (java.util.Map.Entry)를 재정의 할 수 없습니다. 오버라이드 (override)되는 메소드는 java.io.IOException를 슬로우하지 않습니다.
IOExecptio 컴파일러는 IOexception 및 Filenotfoundexception을 처리 할 것을 요구하지 않습니다. 다른 방법이 있습니까? Pls는 예제 코드를 보여 주며, 나는 자바에서 새로 왔으며 단지 2 레벨 캐싱의 기본 원리를 이해하려고 노력했다. Thx
BTW super.removeEldestEntry (장남)이 항상 false를 반환 여기
당신이 원하는 것을 할 수 있습니다 무언가이다. 가장 오래된 항목을 삭제하려는 경우 true를 반환하도록 재정의해야합니다. 파일 t.tmp는 마지막으로 삭제 된 항목 만 보유합니다. 이게 니가 의도 한거야? –