PreferencesUtils { public static void addToStringSet(Context context, String key, String value) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); Set<String> newSet = new HashSet<>(preferences.getStringSet(key, Collections.emptySet())); newSet.add(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 addToStringSet() { String value = "1"; PreferencesUtils.addToStringSet(context, TEST_KEY, value); Set<String> result = PreferencesUtils.getStringSet(context, TEST_KEY); assertThat(result, contains(value)); }