0

I am stuck on something which first seemed very easy to me, but I just cannot figure it out. I want to store an array of integers (int[]) in SharedPreferences, preferably as a set and not as separate entries in SharedPreferences, because this way I am 100% sure that the order of this int[] will remain the same:

int[] myIntArray = new int[]{1,2,3};
Set valueSet = new HashSet(asList(myIntArray));

SharedPreferences sharedPreferences = context.getSharedPreferences("name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putStringSet("key", valueSet);
editor.commit();

When I return this set

SharedPreferences sharedPreferences = context.getSharedPreferences("name",Context.MODE_PRIVATE);
Set set = sharedPreferences.getStringSet("key", null);

In debugging I end up with a set which is structured as follows:

->set {HashSet}
----->0 {int[]}
---------->0 = 1
---------->1 = 2
---------->2 = 3           

I just cannot figure out how I can retrieve this int[] from within this set back into a local int[] variable.

The closest I get to my goal is by doing:

Set set = sharedPreferences.getStringSet("imageverdict", null);
Object[] o = set.toArray();
Object o1 = o[0];

This gives me in debugging

->o1 {int[]}
---->0 = 1
---->1 = 2
---->2 = 3

But when I try to retrieve any values from this by doing int i = o1[1]; I get an error in the editor: Array type expected; found 'java.lang.object'.

This seems weird to me since during debugging o1 is shown as an int[], whilst within the editor android studio tells me it is an object.

Any help is appreciated!

daan166
  • 300
  • 1
  • 10
  • I have some bad news for you: HashSet does not preserve the order of elements. When you try to iterate over a HashSet, its order is undefined. You will have another problem even if you solve this. – ram Dec 26 '17 at 22:36
  • I deleted my answer as @ram is right, please see this https://stackoverflow.com/questions/7175880/how-can-i-store-an-integer-array-in-sharedpreferences – nabil london Dec 26 '17 at 22:43
  • I see. However, in my case I store the int[] within a list which is stored as a HashSet. The fact that o1 shows all my variables in the right order indicates that this should be possible. The thing is, however, that I only manage to get this as an object and I just cannot manage to transform this object into a int[] – daan166 Dec 26 '17 at 22:50
  • @daan166 it might work for short arrays, but when it is larger (especially when there are multiple elements with identical hash value), you will definitely have problems. – ram Dec 26 '17 at 22:52
  • @daan166 Did you see my answer? – nabil london Dec 26 '17 at 22:56
  • @ram thanks, although it seems rather poor that it is impossible I understand. What alternatives could I have? Create own database file? Yes nabillondon, thank you! I wish I had found that topic earlier and not after spending my afternoon and evening trying to chase the impossible. – daan166 Dec 26 '17 at 23:03

0 Answers0