SharedPreferences is a class in the android.content package that allows developers to store and retrieve key-value pairs of primitive data types in a shared preference file. One of the methods available in this class is getStringSet, which retrieves a set of string values from the shared preference file.
Example 1:
Suppose that we want to store a set of usernames for our application. We can use SharedPreferences to achieve this:
// In MainActivity.java
// Obtain a SharedPreferences instance SharedPreferences preferences = getSharedPreferences("my_preferences", MODE_PRIVATE);
// Define a set of usernames Set usernames = new HashSet<>(); usernames.add("user1"); usernames.add("user2"); usernames.add("user3");
// Store the set of usernames in the shared preference file SharedPreferences.Editor editor = preferences.edit(); editor.putStringSet("usernames", usernames); editor.apply();
// Retrieve the set of usernames from the shared preference file Set storedUsernames = preferences.getStringSet("usernames", new HashSet<>());
// Display the set of usernames Log.d("MainActivity", "Stored usernames = " + storedUsernames.toString());
In this example, we first obtain a SharedPreferences instance with the name "my_preferences" and mode MODE_PRIVATE. We then define a set of usernames using a HashSet and store it in the shared preference file using the putStringSet method of SharedPreferences.Editor. Finally, we retrieve the set of usernames by calling the getStringSet method of SharedPreferences and display them in the logcat.
Example 2:
Suppose that we want to store a set of default options for our application. We can use SharedPreferences to achieve this:
// In SettingsActivity.java
// Obtain a SharedPreferences instance SharedPreferences preferences = getSharedPreferences("my_preferences", MODE_PRIVATE);
// Define the default options Set defaultOptions = new HashSet<>(); defaultOptions.add("Option1"); defaultOptions.add("Option2");
// Store the default options in the shared preference file if (!preferences.contains("options")) { SharedPreferences.Editor editor = preferences.edit(); editor.putStringSet("options", defaultOptions); editor.apply(); }
// Retrieve the options from the shared preference file Set options = preferences.getStringSet("options", new HashSet<>());
// Display the options in a list view ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(options)); ListView listView = findViewById(R.id.listView); listView.setAdapter(adapter);
In this example, we first obtain a SharedPreferences instance with the name "my_preferences" and mode MODE_PRIVATE. We then define a set of default options using a HashSet and store it in the shared preference file using the putStringSet method of SharedPreferences.Editor, only if the options key does not already exist in the file. Finally, we retrieve the options by calling the getStringSet method of SharedPreferences and display them in a list view using an ArrayAdapter.
The package library for SharedPreferences is "android.content".
Java SharedPreferences.getStringSet - 30 examples found. These are the top rated real world Java examples of android.content.SharedPreferences.getStringSet extracted from open source projects. You can rate examples to help us improve the quality of examples.