I'm wrapped LinkedHashMap<String, LinkedHashMap<Date, Double>>
into a List with;
List<LinkedHashMap<String, LinkedHashMap<Date, Double>>> list = new ArrayList(mainCodesMap.entrySet());
which mainCodeMap
is type of 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 (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;
java.lang.ClassCastException: java.util.LinkedHashMap$Entry cannot be cast to java.util.LinkedHashMap
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.
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.
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