Java – Add items to Set Java in the correct order

Add items to Set Java in the correct order… here is a solution to the problem.

Add items to Set Java in the correct order

I’m creating a song playback app. I have a collection saved in SharedPreferences. I want to add and remove filenames from it and make them in the same order as when I added them, for example:

recentsp = getSharedPreferences("recentkey", Context.MODE_PRIVATE);

recent = recentsp.getStringSet("recent", recent);
    recentedited = recent;

if (recentedited.contains(string) {
        recentedited.remove(string);
        Log.i(TAG, "Recent exists, removing song");
        SharedPreferences.Editor editor = recentsp.edit();
        editor.clear();
        editor.putStringSet("recent", recentedited);
        editor.commit();
    }

recentedited.add(string);
    Log.i(TAG, "adding song to recent");
        SharedPreferences.Editor editor = recentsp.edit();
        editor.clear();
        editor.putStringSet("recent", recentedited);
        editor.commit();

But when I look at these in the ListView, the problem arises. I want them to be in the order I added them so that I can have a recently played section, but sometimes they don’t move at all, or they may end at the beginning. This seems to be random. Am I missing something?

Edit:

I checked to make sure SharedPreferences is not empty by doing this in the initialization step….

Edit:

Even with LinkedHashSet, I still can’t get the sort right. I only call SharedPreferences when it’s null, so I’m not sure how to make sure I’m using LinkedHashSet.

        recentsp = getSharedPreferences("recentkey", Context.MODE_PRIVATE);
    recent = recentsp.getStringSet("recent", null);
    if (recent == null) {

recent = new LinkedHashSet<String>();
        SharedPreferences.Editor editor = recentsp.edit();
        editor.clear();
        editor.putStringSet("recent", recent);
        editor.commit();
    }

Solution

If you are looking for an implementation of the Set interface that preserves the order of inserts, switch from HashSet to: LinkedHashSet. All standard Set behavior remains the same, but insert-order iterations are added.

From the relevant JavaDoc:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashSet, without incurring the increased cost associated with TreeSet. It can be used to produce a copy of a set that has the same order as the original, regardless of the original set’s implementation

Related Problems and Solutions