简体   繁体   English

java.lang.Boolean不能转换为java.util.LinkedList

[英]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 . 我有一个HashMap ,其中键的类型为String ,值的类型为LinkedList ,类型为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. 正如您所看到的,问题在于向Map添加新元素的行 - 方法add返回一个布尔值,这就是添加到Map的内容。 The code above fixes the problem and adds what you intended to the Map - a LinkedList. 上面的代码修复了问题,并将您想要的内容添加到Map中 - 一个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: 根据下面的注释 - 假设您实际上可以ArrayList替换LinkedList (对于某些操作可能更快),并且您使用的唯一 LinkedList特定方法是addLast (这是add的同义词),上面的代码可以按照以下方式重写,使用接口而不是容器的具体类,更加面向对象:

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 . List.add返回boolean ,它被自动装箱为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. 你的else子句正在创建一个LinkedList,调用一个返回boolean的方法( add ),并将生成的autoboxed布尔值放入map中。

Do you know about generics? 你知道泛型吗? You should type w as a Map<String,List<String>> instead of just a Map. 您应该键入w作为Map<String,List<String>>而不仅仅是Map。 If you did that, this error would have been caught at compile time. 如果您这样做,则会在编译时捕获此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法将java.lang.Boolean强制转换为java.util.Map - java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map java.util.LinkedList中的addFirst() - addFirst() in java.util.LinkedList 首选项不能将java.lang.boolean强制转换为String - Preference cannot cast java.lang.boolean to String Cursor $ DefaultCursor不能强制转换为java.lang.Boolean吗? - Cursor$DefaultCursor cannot be cast to java.lang.Boolean? JRException:java.lang.ClassCastException:java.lang.String无法转换为java.lang.Boolean - JRException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean java.util.LinkedList中 <ClassName> 无法转换为ClassName - java.util.LinkedList<ClassName> cannot be converted to ClassName Java compareTo方法错误:java.lang.Integer无法转换为java.lang.Boolean - Java compareTo method Error :java.lang.Integer cannot be cast to java.lang.Boolean java.lang.ClassCastException:无法将java.lang.Boolean强制转换为org.apache.pig.data.Tuple - java.lang.ClassCastException: java.lang.Boolean cannot be cast to org.apache.pig.data.Tuple LibGDX中的图块无法将java.lang.String转换为java.lang.Boolean - Tiled in LibGDX cannot cast java.lang.String to java.lang.Boolean React native - 如何修复 Java.lang Double 无法转换为 java.lang.Boolean - React native - how to fix Java.lang Double cannot be cast to java.lang.Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM