Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中快速搜索列表 问题描述_Java_Android_Algorithm_Search_Full Text Search - Fatal编程技术网

Java 在Android中快速搜索列表 问题描述

Java 在Android中快速搜索列表 问题描述,java,android,algorithm,search,full-text-search,Java,Android,Algorithm,Search,Full Text Search,我有一个字符串列表,其中包含8000项。包含列表的项目如下所述 List<String> stringList = new List<String>(8000); stringList.add("this is first string."); stringList.add("text which I want to search."); stringList.add("separated string items.&qu

我有一个字符串列表,其中包含8000项。包含列表的项目如下所述

List<String> stringList = new List<String>(8000);
stringList.add("this is first string.");
stringList.add("text which I want to search.");
stringList.add("separated string items.");
....
所以你可以看到,我列表中的每一项都是一个句子,有三个以上的单词

问题 外部用户可以通过以下方式搜索列表。例如,用户要首先搜索单词,搜索算法必须以这种方式工作

搜索算法必须在列表中运行,首先将单词与句子中的所有单词进行比较,如果句子中的任何单词以“第一”开头,则必须返回该句子。为了实现这个算法,我写了下面的代码,你们可以看到下面的代码

我实现的算法运行速度非常慢,所以我想知道是否有更快的算法,或者如何使我的算法更快

代码示例 我会持有一张地图,其中每个单词都是一个键,值是包含这个单词的句子的索引

我会持有一张地图,其中每个单词都是一个键,值是包含这个单词的句子的索引

您可以将该方法与一起使用,而不是拆分字符串并搜索每个标记

String currItem = stringListIter.next();
if(currItem.startsWith(textToFind.concat(space))){
    retList.add(currItem);
} else if(currItem.endsWith(space.concat(textToFind))){
    retList.add(currItem);
} else if(currItem.contains(space.concat(textToFind).concat(space))){
    retList.add(currItem);
} else if(currItem.equals(textToFind)){
    retList.add(currItem);
}
First if-检查它是否是第一个单词

第二个if-检查它是否是最后一个单词

<P>第三,如果检查它在中间的某个地方。< /P> Last if-检查它是否是唯一的单词。

您可以将该方法与一起使用,而不是拆分字符串并搜索每个标记

String currItem = stringListIter.next();
if(currItem.startsWith(textToFind.concat(space))){
    retList.add(currItem);
} else if(currItem.endsWith(space.concat(textToFind))){
    retList.add(currItem);
} else if(currItem.contains(space.concat(textToFind).concat(space))){
    retList.add(currItem);
} else if(currItem.equals(textToFind)){
    retList.add(currItem);
}
First if-检查它是否是第一个单词

第二个if-检查它是否是最后一个单词

<P>第三,如果检查它在中间的某个地方。< /P>
最后一个if-检查它是否是唯一的单词。

非常适合的任务。

非常适合的任务。

内存中的数据结构中存储了大量数据。为什么不使用数据库?@Rajesh是Sqlite数据库?是的,有很多数据要存储在内存数据结构中。为什么不使用数据库?@Rajesh-An-Sqlite数据库?是的,Contains的工作方式不同于start-with,ifcurrItem.containstexto快速查找此工作,但这不是我的情况。@ViToBrothers-检查更新!我希望它能解决你的问题。你也可以使用正则表达式,但我不是正则表达式方面的专家,所以在这种情况下,你需要向其他地方寻求帮助!Contains的工作方式与start with不同,ifcurrItem.CONTAINSTEXT快速查找此工作,但这不是我的情况。@ViToBrothers-检查更新!我希望它能解决你的问题。你也可以使用正则表达式,但我不是正则表达式方面的专家,所以在这种情况下,你需要向其他地方寻求帮助!
for(String s : yourList){
    if(s.contains(textToFind)){
        retList.add(s);
    }
}