2

I have a HashMap that has been generated in an activity and I want to store it using something similar to SharedPreferences. I've tried to find information and have been referenced to something called GSON but I'm not exactly sure what that is. If I have something like:

HashMap<String, String> hashMap = new HashMap<String, String> ();
hashMap.put("String 1", "Value 1");
hashMap.put("String 2", "Value 2");

How can I store hashMap so that I can then read it in another activity and use that information? It also needs to be stored should the user close the app.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
scibor
  • 983
  • 4
  • 12
  • 21

3 Answers3

2

Json is very similar to HasMap. To save a Json you have to use a key and value like HasMap:

JSONObject userDetail = new JSONObject();

        userDetail.put("email", email);
        userDetail.put("token", token);

After that you can use a FileWriter or FileInputStream to save it in file .json and you can get it from other activitys using a JSONParser.

For more information about json look this

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
2

Go with Victor's answer.

But if your hashMap is complex, like(hash inside hash of hash) you can store it directly in a file and read it later:

Write to file:

    public void saveHashToFile(HashMap<String, Object> hash) throws IOException{
        String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/your.properties";
        File file = new File(filePath);
        FileOutputStream f = new FileOutputStream(file);
        ObjectOutputStream s = new ObjectOutputStream(f);
        s.writeObject(getProperty_Global());
        s.close();  


    }

Reading back from file:

      public HashMap<String, Object> getLocalHashFromFile() throws OptionalDataException, ClassNotFoundException, IOException{

        String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/your.properties";
        File file = new File(filePath);
        FileInputStream f = new FileInputStream(file);

        ObjectInputStream s = new ObjectInputStream(f);

        HashMap<String, Object> hashFromFile=(HashMap<String, Object>) s.readObject();
        s.close();
        Log.e("hashfromfileLOCAL", ""+hashFromFile);

        return hashFromFile;
    }
amalBit
  • 12,041
  • 6
  • 77
  • 94
1

Gson is a Java library for converting Java objects into JSON strings and vice-verse. I had the same issue in my project, and used Gson for converting the HashMap into a String, saving it into SharedPreferences, and getting it back in another activity.

To store the Map:

SharedPreferences preferences = getSharedPreferences("com.your.package", MODE_PRIVATE);
Type genericType = new TypeToken<HashMap<String, String>>() {}.getType();
String serializedHashMap = Helpers.serializeWithJSON(your_hashmap, genericType);
preferences.edit().putString("Somename", serializedHashMap).commit();

serializeWithJSON():

public static String serializeWithJSON(Object o, Type genericType) {
    Gson gson = new Gson();
    return gson.toJson(o, genericType);
}

To deserialize:

Gson gson = new Gson();
Type genericType = new TypeToken<HashMap<String, String>>() {}.getType();
HashMap<String, String> hashMap = gson.fromJson(preferences.getString("Somename", "Errormessage"), genericType);

To persistently store it in a file, use amalBit's answer.

Lennart
  • 9,657
  • 16
  • 68
  • 84