用于搜索字符串的更快的数据结构

Mar*_*ska 8 java data-structures

我有这个代码,用于确定单词(忽略大小写)是否包含在wordList文本文件中.但是,wordList文本文件可能有65000 ++行,并且只使用我的实现在下面搜索单词需要将近一分钟.你能想到更好的实施吗?

谢谢!

import java.io.*;
import java.util.*;

public class WordSearch 
{
    LinkedList<String> lxx;
    FileReader fxx;
    BufferedReader bxx;

    public WordSearch(String wordlist) 
        throws IOException
    {
        fxx = new FileReader(wordlist);
        bxx = new BufferedReader(fxx);
        lxx = new LinkedList<String>();
        String word;

        while ( (word = bxx.readLine()) != null) 
            {
            lxx.add(word);
        }

        bxx.close();
    }

    public boolean inTheList (String theWord)
    {
        for(int i =0 ; i < lxx.size(); i++)
            {
            if (theWord.compareToIgnoreCase(lxx.get(i)) == 0)
                    {
                return true;
            }
        }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

Aas*_*set 14

使用HashSet您在其中放置每个单词的小写版本.检查a是否HashSet包含指定的字符串,平均是一个常量时间(读取:极快)操作.