0

How to save and retrieve data from shared preference by input order in java?

When i save such data in shared preferences

 HashMap<Integer, String> likes = new HashMap<Integer, String>();

    // THIS IS INPUT ORDER

    likes.put(1,"a")
    likes.put(2,"b")
    likes.put(3,"c")
    likes.put(4,"d")
        editor = getSharedPreferences("PREFS_NAM", 0).edit();
        for (Map.Entry<Integer, String> entry : likes.entrySet())
         editor.putString(String.valueOf((entry.getKey())), 
         entry.getValue());
        editor.apply();

when i retrieve it

SharedPreferences prefs = getSharedPreferences("PREFS_NAM", 0);
    for (Map.Entry entry : prefs.getAll().entrySet()) 
           likes.put(Integer.valueOf(entry.getKey().toString()), 
           entry.getValue().toString());

it looks like

(1,"a")
(2,"b")
(4,"d")
(3,"c")

which not same input order!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dreamer
  • 517
  • 1
  • 4
  • 11

2 Answers2

1

try the following Map<Integer, String> likes = new LinkedHashMap<Integer, String>(); LinkedHashMap was designed for the purpose of keeping keys, values and entries in the order they were put in.

Bahuul
  • 11
  • 1
0

SharedPreferences are designed for storing and retrieving simple data, and it is fairly awkward to use them for even slightly complicated structures.

You appear to be trying to store and retrieve a Map, and you want iteration order to be insertion order. Firstly, you should be using a LinkedHashMap rather than an ordinary HashMap because the latter doesn't make any guarantees about iteration order.

Map<Integer, String> likes = new LinkedHashMap<>();

However that isn't going to mean that the order is preserved when you get the data out of the SharedPreferences.

You could fix this by using keys "K0", "K1", "K2"... to store the map's keys and keys "V0", "V1", "V2"... to store the map's values. Something like this would work (not tested):

int index = 0;
for (Map.Entry<Integer, String> e : likes.entrySet()) {
     editor.putInt("K" + index, e.getKey());;
     editor.putString("V" + index, e.getValue());
     index++;
}

Then to retrieve:

Map<Integer, String> likes = new LinkedHashMap<>();
Map<String, ?> all = prefs.getAll();
for (int i = 0; i < all.size() / 2; i++) 
    likes.put((Integer) all.get("K" + i), (String) all.get("V" + i));

This is obviously hideous, and SharedPreferences wasn't really designed for this, but it works.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • I will try it and give you the answer but i have keys they start from 1 to 400 this will not make unique keys – Dreamer Oct 21 '17 at 22:31
  • @Dreamer Fixed! – Paul Boddington Oct 21 '17 at 22:36
  • Cannot define "get" in likes.put((Integer) i.get("K" + i), (String) i.get("V" + i)); – Dreamer Oct 21 '17 at 22:44
  • Are there other things in the SharedPreferences? – Paul Boddington Oct 21 '17 at 22:51
  • Nothing there , i have array that contains values and sequence of these valuse for example "wwewew","dsdsdsdsd","sdsdsds" the sequence is unique i used as key (starting from 1 to end of string array) , the value of string as value – Dreamer Oct 21 '17 at 22:54
  • Did you change `i.get` to `all.get`? `get` is definitely a method of `Map`. – Paul Boddington Oct 21 '17 at 22:59
  • From your comment about the array, it sounds like what you're really trying to do is store a `String[]` in a shared preferences. The whole `Map` thing was a complete red herring. My code definitely compiles as I just tried it. I think I'm going to give up on this one. – Paul Boddington Oct 21 '17 at 23:07
  • Thanks for trying to help , but the only way to fix this is retrieve elements from shared preferences using normal for and index of each set,else nothing work – Dreamer Oct 21 '17 at 23:10