Save ArrayList to SharedPreferences
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
SharedPreferences stores primitive values and strings, not arbitrary Java or Kotlin collections. So if you want to save an ArrayList, you need to convert it into something SharedPreferences can persist, usually JSON or, in limited cases, a Set<String>. The important design question is whether the list is really lightweight settings data or whether it should live in a proper database such as Room.
What SharedPreferences Can Store Directly
SharedPreferences supports values such as:
- '
String' - '
int' - '
long' - '
float' - '
boolean' - '
Set<String>'
That means an ArrayList is never stored directly. It must be serialized first.
The Common Solution: JSON with Gson
For most simple lists, JSON is the cleanest approach.
This works for strings, numbers, and custom objects as long as Gson can serialize them.
Saving Custom Object Lists
The same pattern works for more complex items.
This is fine for small settings-style payloads. It is not a great fit for large, frequently updated domain data.
putStringSet Is More Limited Than It Looks
If the list is only strings, you might consider putStringSet. That can work, but it has a major limitation: sets do not preserve order.
If order matters, ArrayList to JSON is safer. Also, putStringSet only helps for strings, not custom objects.
Handle Missing or Bad Data Gracefully
Stored JSON can become invalid after app upgrades or model changes. Deserialization should fail safely.
This prevents a corrupted preference value from crashing the app at startup.
Know When to Move to Room or DataStore
A useful rule is:
- use
SharedPreferencesfor compact settings - use DataStore for modern key-value preferences
- use Room for structured, queryable, evolving data
If the list is large, relational, or frequently modified, SharedPreferences is the wrong storage model even if JSON serialization technically works.
Java Version
If the codebase is Java rather than Kotlin, the same Gson strategy applies.
Common Pitfalls
- Trying to store an
ArrayListdirectly without serializing it first. - Using
putStringSetwhen list order matters. - Saving large or frequently updated lists in preferences instead of using a database.
- Forgetting
TypeTokenwhen deserializing generic collection types. - Assuming old JSON payloads will keep parsing forever after the model class changes.
Summary
- '
SharedPreferencescannot store anArrayListdirectly.' - JSON serialization with Gson is the most common lightweight solution.
- '
putStringSetis only useful for unordered string collections.' - Add safe fallback behavior for corrupted or old serialized data.
- Move to DataStore or Room when the data stops looking like simple preferences.
Related reading
- Scala equivalent of Java java.lang.ClassT Object
- Scala Programming for Android
- Scalable spring batch job on kubernetes
- Scan components of different maven modules/JARs in a Spring Boot application
- Save custom objects into NSUserDefaults
- Save custom objects into NSUserDefaults
- Scanner is skipping nextLine() after using next() or nextFoo()?
- Scanner vs. BufferedReader

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.