0

I have the following hashmap, that stores string keys and linked list values

public HashMap<String, LinkedList<String>> wordIndex = new HashMap<>();

My wordIndex contains multiple linked lists that contains the word "algorithms", so I wrote the following code to see if my hashmap is able to find the word "algorithms"

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";
 } 
}

However it is always returning none, which means it can find the word in the linked list. So what is the correct procedure to traverse through linked Lists inside Hashmaps. I searched but couldn't find any answer.

Also I need to return all the KEYS, containing the query word (in this case "algorithm"). I cant seem to find a function in the hashmap class that can do that(or maybe I saw it but didn't understand it). I am new to hashmaps could you guys please help me out and point me in the right direction.

asked Nov 26, 2014 at 12:44
1
  • You are checking a String against LinkedList objects, which will never match. Commented Nov 26, 2014 at 12:51

2 Answers 2

3

You can't do that. If you want to check if there is the word in any LinkedList in the HashMap, you should do something like :

String getWord(String query) { //query=user input in this case algorithms
 for(LinkedList<String> l : wordIndex.values()) {
 if(l.contains(query.toLowerCase())) {
 return "hello"; //should return hello if word is found
 }
 }
 return "none"; 
}
answered Nov 26, 2014 at 12:49
2
  • Thanks a lot, so the process is to use a enhanced for loop (or an iterator, would also work I guess). Any Idea on how to get the keys returned? Commented Nov 26, 2014 at 12:54
  • You could use a for loop on wordIndex.entrySet() instead. En Entry is the (Key, Value) pair stored in the map. See : docs.oracle.com/javase/7/docs/api/java/util/… Commented Nov 26, 2014 at 12:59
0
public boolean containsValue(Object value) {
if (value == null)
 return containsNullValue();
Entry[] tab = table;
 for (int i = 0; i < tab.length ; i++)
 for (Entry e = tab[i] ; e != null ; e = e.next)
 if (value.equals(e.value))
 return true;
return false;
}

if you look at the containsValue method you will see it uses Entry object equals method for matching and if you look this Entry class equals method objects other than Entry type it returns false. I think the best way is using contains method of linked list while iterating over map for each record

 public final boolean equals(Object o) {
 if (!(o instanceof Map.Entry))
 return false;
 Map.Entry e = (Map.Entry)o;
 Object k1 = getKey();
 Object k2 = e.getKey();
 if (k1 == k2 || (k1 != null && k1.equals(k2))) {
 Object v1 = getValue();
 Object v2 = e.getValue();
 if (v1 == v2 || (v1 != null && v1.equals(v2)))
 return true;
 }
 return false;
 }
answered Nov 26, 2014 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.