-1

I have an ArrayList that contains Some Dates. I want to save it to user's storage to use it later. here's the code i've tried:

ArrayList<String> dates = new ArrayList<>();
dates.add("1 شهریور 1395");
dates.add("1 شهریور 1395");
dates.add("2 شهریور 1395");
dates.add("2 شهریور 1395");
SharedPreferences preferences;
SharedPreferences.Editor editor;
preferences = getPreferences(Context.MODE_PRIVATE);
editor = preferences.edit();
Set<String> dateSet = new Set(dates);
editor.putStringSet(dateSet);

but when i call dateSet.size() well it's normal that it returns 2... not 4.
how can i save my dates without reducing it's size?

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
  • @Andy then how can i save the arraylist in `sharedPreferences` ? – Mahdi-Malv Sep 30 '16 at 15:43
  • search your comment content on google , you will find many helpful answers,good luck – Pavneet_Singh Sep 30 '16 at 15:45
  • 1
    Your dates are strings. Your strings are 1, 1, 2, 2. When things are added to a Set, a set calls the .equals method to see if the object is already in the set. That is why the list has 4 entries and the Set only has 2: There are two 1's and two 2's making the Set filter a 1 and a 2 out upon insertion. – Sandy Simonton Sep 30 '16 at 15:45
  • @Sandy this is the Actual values in my project. – Mahdi-Malv Sep 30 '16 at 15:48
  • @Pavneet i have tried a lot... found nothing. All was how to remove not ignore removing duplicate values. – Mahdi-Malv Sep 30 '16 at 15:50
  • 1
    @PavneetSingh I don't think you can get around it unless you change the list's values. A Set does not allow duplicate values in it, and duplicates are determined by the .equals method. – Sandy Simonton Sep 30 '16 at 15:52
  • @SandySimonton the values are token from a html source code using Jsoup library. not my decision. – Mahdi-Malv Sep 30 '16 at 15:55

2 Answers2

1

use putString()

Set a String value in the preferences editor, to be written back once commit() or apply() are called.

https://developer.android.com/reference/android/content/SharedPreferences.Editor.html

MahdiY
  • 1,269
  • 21
  • 32
0

Do not use putStringSet(). Serialize the array in some format such as JSON or csv and do putString() instead.

https://stackoverflow.com/a/8287418/6865211

Community
  • 1
  • 1
LKHO
  • 106
  • 6