以正确的顺序将项目添加到集合<String> Java

Add items to a Set<String> in proper order Java

我正在创建一个歌曲播放应用程序。我有一个保存在 SharedPreferences 中的集合。我想从中添加和删除文件名,并保持它们的顺序与我添加它们的顺序相同,例如:

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();

但是当我在 ListView 中查看这些时,问题就出现了。我希望它们按照我添加它们的顺序排列,以便我可以有一个最近播放的部分,但有时它们根本不会移动,或者它们可能会在开头结束。这似乎是随机的。我错过了什么吗?

编辑:

我通过在初始化步骤中执行此操作来检查以确保 SharedPreferences 不为空...

编辑:

即使使用 LinkedHashSet,我仍然无法获得正确的排序。我只在 SharedPreferences 为 null 时调用它,所以我不确定如何确保我使用的是 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();
    }

如果您正在寻找保留插入顺序的 Set 接口的实现,请从 HashSet 切换到:LinkedHashSet。所有标准 Set 行为都保持不变,但增加了插入顺序迭代。

来自相关的 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

也许只使用 ArrayList 而不是 Set - 看起来你已经有代码来确保删除和添加项目(如果它已经在集合中) - 该模式应该适用于 ArrayList。

如果您需要代码将列表序列化为首选项,这个较旧的问题有一个示例 Save ArrayList to SharedPreferences or use something like GSON to store and retrive Store a List or Set in SharedPreferences

I have a Set that I keep in SharedPreferences. I want to add and remove file names from it, and keep them in the same order

那是不可能的。您无法控制 Set 的实现,也不需要订购 Set。您的选择是:

  1. 不用担心顺序,或者

  2. 不要将字符串集与 SharedPreferences 一起使用,而是将您的值存储在单个字符串中(例如,编码为 JSON),或

  3. 不要使用SharedPreferences,而是使用其他一些数据存储选项(例如,SQLite 数据库,JSON 文件)