I have a HashMap
where the key is of type String
and the value is of type LinkedList
of type String
.
So basically, here's what I'm trying to do.
while (contentItr.hasNext()) {
String word = (String) contentItr.next();
if (wordIndex.containsKey(word)) {
LinkedList temp = (LinkedList) w.get(word); //Error occurs here
temp.addLast(currentUrl);
} else {
w.put(word, new LinkedList().add(currentUrl));
}
}
The first time that I add a key,value pair, I receive no error. However, when I try to retrieve the linked list associated with an existing key, I get the following exception:
java.lang.Boolean cannot be cast to java.util.LinkedList.
I don't have a possible explanation of why this exception occurs.
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);
}
}
List.add
returns boolean
, which is being autoboxed to Boolean
. Your else clause is creating a LinkedList, calling a method on it (add
) that returns boolean, and putting the resulting autoboxed Boolean into the map.
Do you know about generics? You should type w as a Map<String,List<String>>
instead of just a Map. If you did that, this error would have been caught at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With