Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.Boolean cannot be cast to java.util.LinkedList

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.

like image 752
lbj-ub Avatar asked Dec 06 '11 04:12

lbj-ub


2 Answers

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);
    }
}
like image 57
Óscar López Avatar answered Oct 03 '22 23:10

Óscar López


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.

like image 41
yshavit Avatar answered Oct 03 '22 22:10

yshavit