Android ArrayList of custom objects – Save to SharedPreferences – Serializable?

Yes, you can save your composite object in shared preferences. Let’s say.. Student mStudentObject = new Student(); SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Editor prefsEditor = appSharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(mStudentObject); prefsEditor.putString(“MyObject”, json); prefsEditor.commit(); ..and now you can retrieve your object as: SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Gson gson = new … Read more

Java: How to read a text file

You can use Files#readAllLines() to get all lines of a text file into a List<String>. for (String line : Files.readAllLines(Paths.get(“/path/to/file.txt”))) { // … } Tutorial: Basic I/O > File I/O > Reading, Writing and Creating text files You can use String#split() to split a String in parts based on a regular expression. for (String part … Read more

ArrayList or List declaration in Java

List<String> arrayList = new ArrayList<String>(); Is generic where you want to hide implementation details while returning it to client, at later point of time you may change implementation from ArrayList to LinkedList transparently. This mechanism is useful in cases where you design libraries etc., which may change their implementation details at some point of time … Read more

How can I calculate the difference between two ArrayLists?

In Java, you can use the Collection interface’s removeAll method. // Create a couple ArrayList objects and populate them // with some delicious fruits. Collection firstList = new ArrayList() {{ add(“apple”); add(“orange”); }}; Collection secondList = new ArrayList() {{ add(“apple”); add(“orange”); add(“banana”); add(“strawberry”); }}; // Show the “before” lists System.out.println(“First List: ” + firstList); System.out.println(“Second … Read more

Moving items around in an ArrayList

I came across this old question in my search for an answer, and I thought I would just post the solution I found in case someone else passes by here looking for the same. For swapping 2 elements, Collections.swap is fine. But if we want to move more elements, there is a better solution that … Read more

Why is processing a sorted array *slower* than an unsorted array? (Java’s ArrayList.indexOf)

It looks like caching / prefetching effect. The clue is that you compare Doubles (objects), not doubles (primitives). When you allocate objects in one thread, they are typically allocated sequentially in memory. So when indexOf scans a list, it goes through sequential memory addresses. This is good for CPU cache prefetching heuristics. But after you … Read more

Passing ArrayList through Intent

In your receiving intent you need to do: Intent i = getIntent(); stock_list = i.getStringArrayListExtra(“stock_list”); The way you have it you’ve just created a new empty intent without any extras. If you only have a single extra you can condense this down to: stock_list = getIntent().getStringArrayListExtra(“stock_list”);