2

Why does SharedPreferences.Editor have a method putStringSet() but not a method putStringList()? This doesn't make any sense to me. My understanding (which could be wrong) is that SharedPreferences objects are stored internally using xml, and xml supports arrays.

I understand that a SharedPreference object is intended to be a simple object for small amounts of data, so you wouldn't want to have too many put methods, but I would have thought if you were going to have one of putStringSet() or putStringList(), putStringList() would be both more generally useful (a set can be saved as a list, but not vice-versa) and easier to implement. Can anyone explain this?

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • check this one help you http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Pavan Sep 14 '15 at 16:06
  • @Pavan the OP is asking WHY putStringSet is supported but not putStringList, not for how to do it. My guess would be something to do with the fact that sets are unordered collections unlike lists; interesting question, hopefully someone can elaborate – rcbevans Sep 14 '15 at 16:08
  • @Pavan That link contains several work-arounds (e.g. convert to Json string, use `putString(key + index, strings.get(index);` or use a separation character like `string0 + '#' + string1 + '#' + string2` etc). I've been using these hacks, but I'd like to know why it was designed this way. It makes no sense to me at all. – Paul Boddington Sep 14 '15 at 16:12
  • I am voting to close this as primarily opinion-based. The only people who can definitively answer "why" would be the developers responsible for `SharedPreferences`, and they are unlikely to see this question, let alone answer it. – CommonsWare Sep 14 '15 at 16:21
  • @CommonsWare Fair enough. This site is a democracy, but I don't think it is opionion-based. There must be a technical reason why sets of strings are easier to save than lists of strings. I'd like to know what that reason is. – Paul Boddington Sep 14 '15 at 16:23
  • "There must be a technical reason why sets of strings are easier to save than lists of strings" -- who said it *is* easier? Just because they chose to do X does not mean that X is somehow easier than Y. We do not know why they chose X, and guesses for the reason (e.g., they wanted an API with greater flexibility regarding storage implementation) are merely opinions. – CommonsWare Sep 14 '15 at 16:26
  • @CommonsWare The trouble is that it seems you have to know something about the implementation details here to be able to use the class correctly. There seems to be restrictions on the characters (e.g. questions like http://stackoverflow.com/questions/32530733/android-sharedpreferences-ioexception-error). I could be wrong about there being restrictions here, but I do know that questions like that one are almost never satisfactorily answered on SO. Android documentation (in contrast to java documentation) rarely gives useful information or helpful advice on workarounds. – Paul Boddington Sep 14 '15 at 16:36
  • I am not arguing that the docs suck. Why do you think my book is 3300+ pages? :-) However, the fact that the docs suck does not somehow make "why did the developers of FOO implement BAR as BAZ instead of GOO?" somehow appropriate for this site. Stack Overflow is for questions that can be reasonably authoritatively answered, and "why did they do that?" questions rarely can. – CommonsWare Sep 14 '15 at 16:42
  • @CommonsWare I think I need to give your book a look, but it sounds like I'll have to reinforce my shelves first. – Paul Boddington Sep 14 '15 at 17:05
  • 1
    It's digital. Just don't print it out, or if you do, remember to lift with your knees, not with your back. :-) – CommonsWare Sep 14 '15 at 17:07

1 Answers1

2

I am not an implementor of the source but if you think about it, the KV store that SharedPreferences are is pretty barebones. So you have putString() and you have put StringSet(). Using StringSet variants to save multiple strings put no constraints on the actual storage solution. i.e. if instead of an XML file it were a sqlite db, you could query the table for all values with the key and that type. Whatever way the sql engine ordered them would be taken as is. However if you had to maintain order a new structure that would impose onto all other data types would have to be introduced, like an order column. Even in XML this would require more storage and more complexity to denote that one is ordered and one is not.

It seems to me that as long as the SharedPreferences supports putString and putStringSet you can achieve both ordered and non ordered storage.

i.e.

String orderedString = TextUtils.join(mySafeDeliminator, myListOfStrings);
preferences.edit().putString("key", orderedString).apply();

....
String orderedReturnedString = preferences.getString("key");
List<String> listOfStrings = Arrays.asList(TextUtils.split(orderedReturnedString, mySafeDeliminator));
Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64