次のコードを使用して、PC アプリケーションで HashMap をシリアル化しています。
private void serialize(HashMap<Integer, Integer> map2write, String name_ser)
{// serializes fphlist into .ser file called name_ser
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(project_dir + "/" + name_ser + ".ser");
} catch (FileNotFoundException ex) {
Logger.getLogger(AdminConsoleUI.class.getName()).log(Level.SEVERE, null, ex);
}
ObjectOutputStream out;
try {
out = new ObjectOutputStream(fileOut);
out.writeObject(map2write);
out.reset();
out.flush();
out.close();
fileOut.close();
} catch (IOException ex) {
Logger.getLogger(AdminConsoleUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
次に、次のコードを使用して、Android アプリケーションでシリアル化を解除しています。
private HashMap<Integer,Integer> deserialize_Map(String fn)
{// deserializes fn into HashMap
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
try
{
FileInputStream fileIn = new FileInputStream(project_dir + "/" + fn + ".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
hm = (HashMap<Integer,Integer>) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
Log.e("MYAPP", "exception", i);
return null;
}catch(ClassNotFoundException c)
{
Log.e("MYAPP", "exception", c);
return null;
}catch(ClassCastException ex)
{
Log.e("MYAPP", "exception", ex);
return null;
}
return hm;
}
結局、私は2つの問題に直面しています。
1) デシリアライズには非常に長い時間がかかります。約数千のキーが含まれているので、正常ですか? それに取り組むためのより効率的なシリアル化方法はありますか?
2) デシリアライゼーションの後、VM で最初に占有するサイズのほぼ 2 倍を占めるハッシュマップを取得し、デバッガーでチェックすると、本来含まれているはずのキー値の間に多くの null エントリがあります。ただし、それらは null キーではなく null であり、その内容を表示することはできません。私はEclipseでデバッグします。なぜそれが起こるのでしょうか?