how to store and retrieve (key,value) kind of data

2020-02-29 06:37发布

问题:

I have a hash map table as below,

HashMap<String, String> backUpCurency_values = new HashMap<String, String>();

and i want to store these value for future use.. how can i do that?

Edit:
i will store to Country names and currencyvalue as key and value pair...

回答1:

You should just use a for-each loop and iterate through the map like this:

SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();

for( Entry<String, String> entry : backUpCurency_values.entrySet() ) 
  editor.putString( entry.getKey(), entry.getValue() );

editor.commit();

Then when you need to get your values back for later use you do the following (provided that this SharedPreference-object is reserved for currency):

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);

for( Entry<String, ?> entry : prefs.getAll().entrySet() ) 
  backUpCurency_values.put( entry.getKey(), entry.getValue().toString() );


回答2:

You can use SharedPreferences.

settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();

//The below step you can repeat to put all the key value pair from the hashmap to the shared preference

editor.putString("Key", Value);

// Commit the edits!
editor.commit();

And to retrieve later use

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getString(<Key>, <defaultvalue>);


回答3:

There are a few different ways to approach this. With nothing more to go on than the info that you want to store a HashMap to SharedPreferences, I can only make assumptions.

First thing I'd ask is whether you will be storing other things in the SharedPreferences as well- I'll assume you will.

Here is how I would approach it:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("backUpCurency", stringify(backUpCurency_values));
    editor.commit();

You may want to see what stringify does:

   // turns a HashMap<String, String> into "key=value|key=value|key=value"
   private String stringify(HashMap<String, String> map) {
       StringBuilder sb = new StringBuilder();
       for (String key : map.keySet()) {
           sb.append(key).append("=").append(map.get(key)).append("|");
       }
       return sb.substring(0, sb.length() - 1); // this may be -2, but write a unit test
   }

Then you can just parse that string with known structure upon reading the shared preferences later.

   private HashMap<String, String> restoreIt() {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      String backup = settings.getString("backUpCurency", "");
      HashMap<String, String> map = new HashMap<String, String>();
      for (String pairs : backup.split("|")) {
         String[] indiv = pairs.split("=");
         map.put(indiv[0], indiv[1]);
      }
      return map;
   }


回答4:

You can use SharedPreference like this:

SharedPreferences s_pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor edit=s_pref.edit();

edit.putString("key","value");
edit.commit();

later you can use it like:

String s=s_pref.getString("key","default value");

But,you must have list of keys you have saved values with,into SharedPreferences so that you can get values easily at the time of retrieving them.



回答5:

To store the values use this code

SharedPreferences preferences = getSharedPreferences(
            PREF_FILE_NAME, MODE_PRIVATE);
    if (value.equals("")) {

        boolean storedPreference = preferences.contains(key);
        if (storedPreference) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.remove(key); // value to store
            Log.d("KEY",key);
            editor.commit();
        }
    }else{

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value); // value to store
        Log.d("KEY",key);
        editor.commit();
    }

To retrieve the values use this code

SharedPreferences preferences = getSharedPreferences(
            PREF_FILE_NAME, MODE_PRIVATE);
    Map<String, String> map = (Map<String, String>) preferences.getAll();
    if(!map.isEmpty()){
        Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
             Map.Entry pairs = (Map.Entry)iterator.next();
                pairs.getKey()+pairs.getValue();
                      //write code here
        }
    }