You can simply use a loop / loop that passes all elements and converts them one by one to the type you want, thus creating a new loop% of that type. The conversion to Set
is done by calling String
and back to integer doing with toString()
.
Save Integer.parseInt()
Set<Integer> ints = new HashSet<>(); //o seu Set
Set<String> intsEmString = new HashSet<>();
for (Integer i : ints){
intsEmString.add(i.toString()); //guardar a representação em String de cada um
}
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("meu_set", intsEmString); //guardar o novo Set<String>
editor.commit();
Read Set
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
//ler o Set<String> guardado
Set<String> intsEmString = sharedPref.getStringSet("meu_set", null);
Set<Integer> ints = new HashSet<>(); //criar o novo que vai receber os valores
if (intsEmString != null){
for(String s : intsEmString){
ints.add(Integer.parseInt(s)); //converter de volta cada um com parseInt
}
}
//utilizar o Set<Integer> restaurado