Can't figure it out why set.addAll is adding my list with the orders switched. (still my set is declared as LinkedHashSet)
The code is about getting a Set-List from SharedPreferences, convert it into an ArrayList, add one item to list and then add the whole list to Set.
The code looks something like this:
Set<String> set = new LinkedHashSet<String>();
List<String> stringList = new ArrayList<String>();
///
set = getSharedPrefSet("mySet");
stringList = new ArrayList<String>(set);
stringList.add(message);
set.addAll(stringList); //this messes up my order
saveSharedPrefSet("mySet", set);
The first two strings add correctly (as I debugged it) but the third switches position with the second one.
Output:
The getSharedPrefSet function:
Set getSharedPrefSet (String key) {
sharedPrefs = this.getSharedPreferences("myPreferences", Context.MODE_PRIVATE);
return sharedPrefs.getStringSet(key, null);
}
Workaround
One solution can be found at How can write code to make sharedpreferences for array in android?