title: SharedPreferences - CastException: HashSet cannot be cast to TreeSet
body: <p>I am trying to store <code>TreeSet</code> in the <code>SharedPreferences</code> using the following code:</p>
  
  <pre><code>        Set&lt;String&gt; chemicalValuesSet = new TreeSet&lt;&gt;();
          chemicalValuesSet.add("id: " + checkForNull(jsonChemicalValues.getString("id")));
          editor.putStringSet(SP_CHEMICAL_VALUES, chemicalValuesSet);
          editor.apply();
  </code></pre>
  
  <p>However, when I try to access that <code>TreeSet</code> I am getting casting error, as if this set is declared as a <code>HashSet</code>.</p>
  
  <pre><code>    SharedPreferences sharedPreferences =
              getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
      TreeSet&lt;String&gt; chemicalValues =
              (TreeSet&lt;String&gt;) sharedPreferences.getStringSet(SP_CHEMICAL_VALUES, null);
  </code></pre>
  
  <p>I have no clue to solving this issue. In addition, when I started writing this part I was setting <strong>chemicalValuesSet</strong> as <code>HashSet</code> and retrieving without any problems, afterwards I decided to go with <code>TreeSet</code>s. That's why I have tried cleaning and restarting the project, but still same issues persists.</p>
  
  <p>However, if I simply change type to <code>HashSet</code>
  in the part where I retrieve this set, it works without complaining. </p>