Java集合和映射

Java集合和映射,java,android,list,collections,Java,Android,List,Collections,我正在用Edittext和Listview制作一个android应用程序。在这种情况下,我想使用java集合存储项,然后在该列表集合中搜索项。 我的搜索方法是: 首先,我们将项添加到集合中。 然后,当我们在Edittext中键入单词的起始字母时,该列表将获取以该字母开头的单词的索引,并将选择该索引的listview。 示例:我键入a,然后它将选择苹果,然后。。。。。等等 我想知道什么样的收藏和地图可以帮助我做到这一点? 提前谢谢。我会使用trie。这里的例子很好地解释了这一点。您可以指定一个前缀

我正在用Edittext和Listview制作一个android应用程序。在这种情况下,我想使用java集合存储项,然后在该列表集合中搜索项。 我的搜索方法是: 首先,我们将项添加到集合中。 然后,当我们在Edittext中键入单词的起始字母时,该列表将获取以该字母开头的单词的索引,并将选择该索引的listview。 示例:我键入a,然后它将选择苹果,然后。。。。。等等

我想知道什么样的收藏和地图可以帮助我做到这一点?
提前谢谢。

我会使用trie。这里的例子很好地解释了这一点。您可以指定一个前缀并通过遍历树获取所有单词,也许您应该看看这个

src:


希望这对您有所帮助

如果您只查看键入的第一个字符,而不是单词的后续字符,那么一个简单的解决方案可以是您希望处理的字母表的数组,并让它包含字符串的数组列表。这是非常简单和重量轻,并具有良好的性能,以及见下文:

    // Create an array with the alphabet from 'a' to 'z' (i.e. English alphabet)
    List<String>[] firstCharLookup = new List[26];

    // Initialize the array to avoid NullPointerException's
    for (int i = 0; i < firstCharLookup.length; i++) {
        firstCharLookup[i] = new ArrayList<String>();
    }

    // Fill your collections with the strings in the appropriate collections.
    firstCharLookup[0].add("apple");
    firstCharLookup[0].add("active");
    firstCharLookup[0].add("addict");

    firstCharLookup[1].add("bee");
    firstCharLookup[1].add("busy");
    firstCharLookup[1].add("bus");
    // ...
    // Continue with filling dictionary


    // If user types 'a' or 'A' then lowercase the character, then:
    char firstLookup = 'a';
    System.out.println(firstCharLookup[firstLookup - 'a']);
    // We subtract by 'a' so that we get a '0' based index

    // If user types 'b', then:
    char secondLookup = 'b';
    System.out.println(firstCharLookup[secondLookup - 'a']);
//创建一个字母从“a”到“z”(即英语字母)的数组
List[]firstCharLookup=新列表[26];
//初始化数组以避免NullPointerException
for(int i=0;i