How to save HashMap to Shared Preferences?

jibysthomas picture jibysthomas · Oct 30, 2011 · Viewed 81.8k times · Source

How can I save a HashMap to Shared Preferences in Android?

Answer

Kirill Rakhman picture Kirill Rakhman · Oct 30, 2011

I would not recommend writing complex objects into SharedPreference. Instead I would use ObjectOutputStream to write it to the internal memory.

File file = new File(getDir("data", MODE_PRIVATE), "map");    
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(map);
outputStream.flush();
outputStream.close();