Reputation: 6526
I am serializing a HashMap on my PC application using the following code:
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);
}
}
Then I am de-serializing it in my Android application with this code:
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;
}
In the end, I am facing two problems.
1) Deserialization takes a very long time. It contains around thousands of keys so is it normal? Is there a more efficient serialization method to tackle that?
2) After deserialization, I get a hashmap that occupies almost double the size it originally occupies on VM and when I check it in debugger, it has lots of null entries between the key values it should originally contain. However, they are not null keys but just null and I cannot view what is inside them. I debug in Eclipse. Why would it happen?
Upvotes: 4
Views: 582
Reputation: 9300
1) What is the size for the "normal" HashMap in VM size? I don't think that is another solution ( if is there please someone show us) for that problem. You may try sharing a sqlite or something. If you like to colaborate what are you trying to solve we can propose technics that make the job faster
Update
You can try initializing your Map at init time ,as you suggested , with a large capacity if you know the size HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>(SIZE*2);
for the 2. from here
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method
Upvotes: 3
Reputation: 3910
Have you tried serializing the HashMap to a JSON object? Android has pretty good support for deserializing JSON from a file.
Upvotes: 3