Try this instead:
while (contentItr.hasNext()) {
String word = (String) contentItr.next();
if (wordIndex.containsKey(word)) {
LinkedList temp = (LinkedList) w.get(word);
temp.addLast(currentUrl);
} else {
LinkedList temp = new LinkedList();
temp.add(currentUrl);
w.put(word, temp);
}
}
The problem, as you can see, was in the line that adds a new element to the Map - the method add
returns a boolean value, and that's what was being added to the Map. The code above fixes the problem and adds what you intended to the Map - a LinkedList.
As an aside note, consider using generic types in your code, in that way errors like this can be prevented. I'll try to guess the types from your code (adjust them if necessary, you get the idea), let's say you have these declarations somewhere in your program:
Map<String, String> wordIndex = new HashMap<String, String>();
Map<String, LinkedList<String>> w = new HashMap<String, LinkedList<String>>();
List<String> content = new ArrayList<String>();
Iterator<String> contentItr = content.iterator();
With that, the piece of code in your question can be safely written, avoiding unnecessary casts and type errors like the one you had:
while (contentItr.hasNext()) {
String word = contentItr.next();
if (wordIndex.containsKey(word)) {
LinkedList<String> temp = w.get(word);
temp.addLast(currentUrl);
} else {
LinkedList<String> temp = new LinkedList<String>();
temp.add(currentUrl);
w.put(word, temp);
}
}
EDIT
As per the comments below - assuming that you actually can replace the LinkedList
by an ArrayList
(which might be faster for some operations) and that the only LinkedList
-specific method you're using is addLast
(which is a synonym for add
), the above code can be rewritten as follows, in a more Object-Oriented style using interfaces instead of concrete classes for the containers:
Map<String, String> wordIndex = new HashMap<String, String>();
Map<String, List<String>> w = new HashMap<String, List<String>>();
List<String> content = new ArrayList<String>();
Iterator<String> contentItr = content.iterator();
while (contentItr.hasNext()) {
String word = contentItr.next();
if (wordIndex.containsKey(word)) {
List<String> temp = w.get(word);
temp.add(currentUrl);
} else {
List<String> temp = new ArrayList<String>();
temp.add(currentUrl);
w.put(word, temp);
}
}