所以ArrayList「comb」包含長度相等的字符串和某些字符的變體。在最壞的情況下,這個列表可以包含大約100,000個字。函數checkWord(String str)將一個單詞作爲參數,並檢查該單詞是否存在於Hashtable字典中(其中包含另外90,000個字,文本文件已讀入此散列表)。所以基本上,代碼需要檢查List「comb」中的哪些單詞出現在HashTable「dictionary」中。在最壞的情況下,這個搜索需要花費5分鐘。我想實現Runnable並將其並行化,但不確定如何去做。如何正確實現Runnable在Hashtable中搜索元素?
例如:列表梳包含CURMUDGEON的各種拼寫錯誤和正確的單詞本身。該列表包含98415個。 CURMUEGEON CURMUEGEOH CURMUEGEOJ CURMUEGEKN等等。因此,檢查這些單詞是否存在於哈希表中需要200秒。我想打倒這個時候
class key implements Runnable{
public static ArrayList<String> comb;
public static Hashtable<String,String> dictionary;
public static void main(String[] args) throws IOException{
key obj = new key();
Thread thread1 = new Thread(obj);
thread1.start();
}
public static Boolean checkWord(String str){
String toCheck = str.toLowerCase();
if(dictionary.contains(toCheck)){
return true;
}
else
return false;
}
public void run(){
for(String x:comb)
if (checkWord(x))
filtered.add(x);
}
在HashMap中查找100,000個單詞應該是秒的順序,如果是這樣的話。做多線程是沒有意義的。你確定'dictionary'確實是一個基於散列表的數據結構嗎?請提供[mcve]。 –
@JonSkeet謝謝你,我編輯和更新了我的問題。 – daipayan
嗯...... 1)爲什麼這是一個'Map'而不是'Set'?什麼是Map的值? 2)並行化的代價是複雜性。如果這些值是不相關的,我們對[set intersection]有更好的算法(http://stackoverflow.com/questions/4642172/computing-set-intersection-in-linear-time)。 – dhke