5

So, I'm trying to store HashMap on Android. I think it's better to use internal storage, but I don't understand how to save HashMap in it and then read it later. Can someone explain how to do that properly, please?

There are counters with their own names and values. I want to load them onсe when some activity was started, work with them (change, delete, add new), and then save that data to use next time. Right now I use HashMap because it's easy to delete/add values.

HashMap<String, Integer> counters;
Roman
  • 5,358
  • 9
  • 34
  • 43

3 Answers3

13

SharedPreferences also store data in key-value pair as hashmap, so why not get all key-values from hashmap and store into map, as it:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
                                                           Context.MODE_PRIVATE);
SharedPreferences.Editor editor= pref.edit();

    for (String s : map.keySet()) {
        editor.putString(s, map.get(s));
    }

To fetch values you can use:

public abstract Map<String, ?> getAll ()

http://developer.android.com/reference/android/content/SharedPreferences.html#getAll%28%29

use:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
                                                           Context.MODE_PRIVATE);
HashMap<String, String> map= HashMap<String, String> pref.getAll();
for (String s : map.keySet()) {
        String value=map.get(s);
        //Use Value
    }

Code is not compiled, so it may have some minor errors, but should work.

Community
  • 1
  • 1
jeet
  • 29,001
  • 6
  • 52
  • 53
4

Try this

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

and Another way is HERE

Community
  • 1
  • 1
Ajay
  • 4,850
  • 2
  • 32
  • 44
1

There's a good example here.

So if your Map is like so:

HashMap<String, byte[]> = new HashMap<>();

The functions look like this:

public void saveHashMap(String key , Object obj) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(obj);
    editor.putString(key,json);
    editor.apply();     // This line is IMPORTANT !!!
}

public HashMap<String,byte[]> getHashMap(String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Gson gson = new Gson();
    String json = prefs.getString(key,"");
    java.lang.reflect.Type type = new TypeToken<HashMap<String,byte[]>>(){}.getType();
    HashMap<String,byte[]> obj = gson.fromJson(json, type);
    return obj;
}

The Generic code looks like this:

public void saveHashMap(String key , Object obj) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(obj);
    editor.putString(key,json);
    editor.apply();     // This line is IMPORTANT !!!
}


public HashMap<Integer,YourObject> getHashMap(String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    Gson gson = new Gson();
    String json = prefs.getString(key,"");
    java.lang.reflect.Type type = new TypeToken<HashMap<Integer,YourObject>>(){}.getType();
    HashMap<Integer,YourObject> obj = gson.fromJson(json, type);
    return obj;
}

Replace YourObject with, well, your Java object.