【发布时间】:2018-01-31 10:39:16
【问题描述】:
我知道如何在 Hashtable 中找到对应键的值,但是如何找到附加到特定值的键?我写了一个循环遍历字符串并查找关键字的函数。它做得很快,但我需要返回找到的值(关键字)的键。这是我目前的方法
public void findKeywords(POITextExtractor te, ArrayList<Hashtable<Integer,String>> listOfHashtables, ArrayList<Integer> KeywordsFound) {
String document = te.getText().toString();
String[] words = document.split("\\s+");
int wordsNo = 0;
int wordsMatched = 0;
System.out.println("listOfHashtables = " + listOfHashtables);
for(String word : words) {
wordsNo++;
for(Hashtable<Integer, String> hashtable : listOfHashtables) {
//System.out.println(hashtable + " found in the document:");
if(hashtable.containsValue(word)) {
//<RETURN KEY OF THAT VALUE>
wordsMatched++;
System.out.println(word);
}
}
}
System.out.println("Number of words in document = " + wordsNo);
System.out.println("Number of words matched: " + wordsMatched);
}
【问题讨论】:
-
顺便说一句,您有什么理由使用
Hashtable而不是HashMap?在现代代码中,后者通常是首选。 -
Jon Skeet 所说的,再加上我觉得这些哈希表应该使用单词作为键,而不是单词作为值。
-
一些关键点。感谢你们。在此期间,我将更改我的代码。另一方面,HashMaps 是否比 Hashtables 更有效,或者选择一个而不是另一个的原因是什么?
-
是的,
HashTable更高效,因为Hashtable和Vector等仅保留用于 Java 的无限向后兼容策略。 -
@dsafasfsafasfsa
are HashMaps more efficient than Hashtables??自己探索这个对你更有益:)