带索引的键值的最佳数据结构

时间:2018-04-19 00:03:46

标签: android data-structures android-recyclerview key-value

我正在使用RecyclerView,其中每一行始终与两个String对象相关联:行标题字符串和数据字符串(单击行时使用)。

目前,我使用两个独立的阵列为我的recyclerview / adapter设置提供此数据。但是,我想清理我的方法并使用一个可以将行标题String和数据字符串关联起来的数据结构。

我最初的想法是(链接)HashMap,但RecyclerView.Adapter使用位置索引,所以我不认为这是最好的方法。

有人可以推荐我在这里使用的数据结构吗?希望Java或Android库中有一些我不知道的好东西。

1 个答案:

答案 0 :(得分:0)

您可以创建自定义数据结构并将其存储在ArrayList中。对于RecyclerView,任何时间复杂度为O(1)的DataStructure都是不错的选择。

您可以执行以下操作

public class DataModel {
    private final String title;
    private final String dataString;
    public DataModel(final String title, final String dataString){
        this.title = title;
        this.dataString = dataString;
    }
    // Create getter methods
}

然后您可以在Adapter中使用它,如下所示:

final List<DataModel> dataModelList = new ArrayList<>();