나는 다음과 같은 코드를 사용하여 내 PC의 응용 프로그램에서의 HashMap를 직렬화하고있는 그런 다음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);
}
}
이 코드 내 안드로이드 응용 프로그램에서 직렬화 끊긴 부하 오전 :
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;
}
을
결국 두 가지 문제점에 직면하고 있습니다.
1) 역 직렬화는 매우 오랜 시간이 걸립니다. 수천 개의 키가 포함되어 있으므로 정상입니까? 거기에 더 효과적인 직렬화 방법이 있습니까?
2) 역 직렬화 후, VM에서 원래 차지하는 크기의 거의 두 배가되는 해시 맵을 얻습니다. 그리고 디버거에서 검사 할 때 원래 들어 있어야하는 키 값 사이에 많은 null 항목이 있습니다. 그러나, 그들은 null 키가 아니라 단지 null이며, 나는 그 안에 무엇이 있는지 볼 수 없습니다. Eclipse에서 디버깅합니다. 왜 그런 일이 일어 났을까요?
감사합니다. 정말 도움이됩니다. 나는 HashMap 매개 변수를 가지고 놀 수 있다는 것을 결코 알지 못했다. 초기화하는 동안로드 요소와 용량을 지정하면 도움이됩니다. – Erol
deserialization 전에 HashMap을 만들고 readObject 메서드를 사용하여 해당 HashMap을 써서 쓸 수 있다면 궁금합니다. 너는 어떤 생각을 가지고 있니? – Erol
큰 해시 맵을로드하여 HashMap 이전의 공간을 예약하십시오. myMap = new HashMap (numberOfElements * 2) ;. 그렇게하지 않으면 LinkedHashMap으로 시도 할 수 있습니다. 내가 올바르게 기억한다면 나는 약간의 문제가 너무 컸고 LinkedHashMap deserial은 안드로이드에서 더 빠르다. (어떤 의미는 가지지 않지만 내 타이밍은 견고하다.) 당신은 그것을 시도 할 수도 있습니다 –
weakwire