【发布时间】:2014-11-26 12:44:01
【问题描述】:
我有以下哈希图,它存储字符串键和链表值
public HashMap<String, LinkedList<String>> wordIndex = new HashMap<>();
我的wordIndex包含多个包含“算法”这个词的链表,所以我写了下面的代码来看看我的hashmap是否能够找到“算法”这个词
String getWord(String query) { //query=user input in this case algorithms
if(wordIndex.containsValue(query.toLowerCase()))
{
return "hello"; //should return hello if word is found
}
else
{
return "none";
}
}
但是它总是返回 none,这意味着它可以在链表中找到单词。 那么遍历Hashmaps中的链表的正确过程是什么。我搜索了但找不到任何答案。
我还需要返回所有包含查询词的 KEYS(在本例中为“算法”)。我似乎无法在 hashmap 类中找到可以做到这一点的函数(或者我可能看到但不理解)。我是 hashmaps 的新手,请你们帮助我并指出正确的方向。
【问题讨论】:
-
您正在根据 LinkedList 对象检查字符串,该对象永远不会匹配。
标签: java linked-list hashmap