I've the following sample code. The application is installed successfully at first time. However, it throws an error on reinstalling.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinkedHashSet<String> planets = new LinkedHashSet<String>();
planets.add("Earth");
SharedPreferences prefs = getPreferences(0);
prefs.edit().putStringSet("planets", planets).commit();
prefs = getPreferences(0);
planets = (LinkedHashSet<String>) prefs.getStringSet("planets", new LinkedHashSet<String>());
}
}
I've pasted the error produced on reinstalling the application below.
Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.LinkedHashSet at com.example.test.MainActivity.onCreate(MainActivity.java:12)
I want to understand why a saved LinkedHashSet
can not be casted back to LinkedHashSet
. And why is it automaically being converted to HashSet
by Android?