我有一個包含大量條目的哈希映射,它是序列化的。如果我在哈希映射中做了一個小改動,它是否需要我完全覆蓋舊文件或者是否存在替代方案?Java更新數據結構更改爲序列化文件
public class HashMapSerial {
public static void main(String[] args) {
HashMap<String,Integer> hash=new HashMap<String, Integer>(100000);
hash.put("hello",1);
hash.put("world", 2);
//+ (100000 -2) entry's
ObjectOutputStream s=new ObjectOutputStream(new FileOutputStream(new File("hash.out")));
s.writeObject(hash); // write the hash map to file
hash.put("hello",10);
s.writeObject(hash); //rewrite the whole hashmap again
}
}
由於更改僅對字符串「hello」,並沒有其他因素,纔有可能更新序列化的文件只爲字符串「hello」,而不是再一次改寫整個HashMap的?