【问题标题】:java.lang.Boolean cannot be cast to java.util.LinkedListjava.lang.Boolean 不能转换为 java.util.LinkedList
【发布时间】:2011-12-06 04:01:24
【问题描述】:

我有一个HashMap,其中键的类型为String,值的类型为LinkedList,类型为String

所以基本上,这就是我想要做的。

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

第一次添加键值对时,我没有收到任何错误。但是,当我尝试检索与现有键关联的链表时,出现以下异常:

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

我无法解释为什么会发生此异常。

【问题讨论】:

  • w是我在开篇描述的一个HashMap。
  • 你能说明它的定义吗?在我看来,它的定义是错误的。
  • 我刚刚定义为HashMap w = new HashMap();
  • 这个问题是一个很好的例子,为什么我们需要声明泛型。

标签: java exception casting linked-list hashmap


【解决方案1】:

试试这个:

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

如您所见,问题出在向 Map 添加新元素的行中 - add 方法返回一个布尔值,这就是添加到 Map 的内容。上面的代码解决了这个问题并将您想要的内容添加到 Map - 一个 LinkedList。

顺便说一句,请考虑在您的代码中使用泛型类型,这样可以防止此类错误。我会尝试猜测您代码中的类型(如果需要,调整它们,您明白了),假设您在程序的某处有这些声明:

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();

这样,您的问题中的一段代码就可以安全地编写出来,避免不必要的强制转换和类型错误,就像您遇到的那样:

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

编辑

根据下面的 cmets - 假设您实际上 可以LinkedList 替换为 ArrayList(这对于某些操作可能更快)并且 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);
    }
}

【讨论】:

  • 是的,很好,但我喜欢@yshavit 强烈建议输入HashMap。大多数容器方法都受益于使用泛型参数声明。
  • 我刚刚编辑了我的代码,以向您展示如何实现这一目标的示例 - 使其适应您的实际需求!
  • 除非您需要任何特定于 LinkedList 的方法,否则我会将其设为 Map&lt;String,List&lt;String&gt;&gt; -- 而不是 Map&lt;String,LinkedList&lt;String&gt;&gt;。在 SO 和其他地方有各种关于接口编码的讨论。顺便说一句,如果您不需要 LinkedList 功能,请考虑使用ArrayList。见stackoverflow.com/questions/322715/…
  • OP 正在使用LinkedList 特定的方法:addLast,所以使用List 是不可能的
  • @Óscar López addLastadd 相同,所以如果这是唯一的 LinkedList 特定方法,我仍然会使用 List
【解决方案2】:

List.add 返回boolean,它被自动装箱到Boolean。您的 else 子句正在创建一个 LinkedList,在其上调用一个返回布尔值的方法 (add),并将生成的自动装箱布尔值放入映射中。

你知道泛型吗?您应该将 w 输入为 Map&lt;String,List&lt;String&gt;&gt; 而不仅仅是 Map。如果你这样做了,这个错误就会在编译时被捕获。

【讨论】:

    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多