android - SharedPreferences - CastException: HashSet cannot be cast to TreeSet -


i trying store treeset in sharedpreferences using following code:

        set<string> chemicalvaluesset = new treeset<>();         chemicalvaluesset.add("id: " + checkfornull(jsonchemicalvalues.getstring("id")));         editor.putstringset(sp_chemical_values, chemicalvaluesset);         editor.apply(); 

however, when try access treeset getting casting error, if set declared hashset.

    sharedpreferences sharedpreferences =             getsharedpreferences(shared_preferences, context.mode_private);     treeset<string> chemicalvalues =             (treeset<string>) sharedpreferences.getstringset(sp_chemical_values, null); 

i have no clue solving issue. in addition, when started writing part setting chemicalvaluesset hashset , retrieving without problems, afterwards decided go treesets. that's why have tried cleaning , restarting project, still same issues persists.

however, if change type hashset in part retrieve set, works without complaining.

you're making false assumptions on how sharedpreferences , editor works. api never guarantees set when calling getstringset() same, or same implementation, 1 stored when calling putstringset(). says can pass set, , can set.

if api documentation says returns set, should not assume it's returning treeset, or hashset. it's returning set. if absolutely need treeset, create 1 , copy items returned set treeset.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -