LinkedHashMap$Entry 不能转换为 java.util.LinkedHashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21215681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LinkedHashMap$Entry cannot be cast to java.util.LinkedHashMap
提问by quartaela
I'm wrapped LinkedHashMap<String, LinkedHashMap<Date, Double>>
into a List with;
我被包裹LinkedHashMap<String, LinkedHashMap<Date, Double>>
在一个列表中;
List<LinkedHashMap<String, LinkedHashMap<Date, Double>>> list = new ArrayList(mainCodesMap.entrySet());
which mainCodeMap
is type of Map<String, Map<Date, Double>>
这mainCodeMap
是类型Map<String, Map<Date, Double>>
the thing is there is no problem with list,however, when I try to get elements of list by index in a for loop like;
问题是列表没有问题,但是,当我尝试在 for 循环中按索引获取列表元素时;
for (int i = 0; i < correMatrix.length; i++) {
LinkedHashMap<String, LinkedHashMap<Date, Double>> entryRow = list.get(i);
LinkedHashMap<Date, Double> entryRowData = (LinkedHashMap<Date, Double>) entryRow.values();
..
..
}
jvm throws ClassCastException
which says;
jvm throwsClassCastException
说;
java.lang.ClassCastException: java.util.LinkedHashMap$Entry cannot be cast to java.util.LinkedHashMap
采纳答案by user253751
List<LinkedHashMap<String, LinkedHashMap<Date, Double>>> list = new ArrayList(mainCodesMap.entrySet());
mainCodesMap.entrySet
returns a Set<Map.Entry<...>>
(not literally ...
). You then create an ArrayList
containing these Map.Entry
s. Because you are using the raw type ArrayList
(instead of ArrayList<something>
) the compiler can't catch this problem.
mainCodesMap.entrySet
返回一个Set<Map.Entry<...>>
(不是字面意思...
)。然后创建一个ArrayList
包含这些Map.Entry
s 的对象。因为您使用的是原始类型ArrayList
(而不是ArrayList<something>
),所以编译器无法捕捉到这个问题。
It looks like you actually meant this:
看起来你的意思是这样的:
List<LinkedHashMap<String, LinkedHashMap<Date, Double>>> list = new ArrayList<>();
list.add(mainCodesMap)
Note: ArrayList<>
means the compiler will automatically fill in the <>. It doesn't work in all contexts.
注意:ArrayList<>
表示编译器会自动填写<>。它不适用于所有情况。