PreferencesUtils { public static void removeFromStringSet(Context context, String key, String value) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); Set<String> newSet = new HashSet<>(preferences.getStringSet(key, Collections.emptySet())); newSet.remove(value); preferences.edit().putStringSet(key, newSet).apply(); }  static void addToStringSet(Context context, String key, String value); static void removeFromStringSet(Context context, String key, String value); @NonNull static Set<String> getStringSet(Context context, String key); static boolean isSetIn(int flagSet, int flag);  }
@Test public void removeFromStringSet() { String[] data = {"1", "2", "3", "4", "a", "5"}; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); preferences.edit() .putStringSet(TEST_KEY, new HashSet<>(Arrays.asList(data))) .apply(); PreferencesUtils.removeFromStringSet(context, TEST_KEY, "a"); Set<String> result = PreferencesUtils.getStringSet(context, TEST_KEY); assertThat(result, not(contains("a"))); assertThat(result, containsInAnyOrder("1", "2", "3", "4", "5")); }