Java tutorial
//package com.java2s; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import java.util.HashSet; import java.util.Set; public class Main { public static final String PREF_FAVORED_MOVIES = "pref_favored_movies"; public static void addToFavorites(final Context context, long movieId) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); Set<String> set = sp.getStringSet(PREF_FAVORED_MOVIES, null); if (set == null) set = new HashSet<>(); set.add(String.valueOf(movieId)); sp.edit().putStringSet(PREF_FAVORED_MOVIES, set).apply(); } }