我有一张地图。我想遍历每个条目并从中获取价值。以下是我正在使用的代码,但出现异常。我不知道我的代码有什么问题。
for(Map.Entry<String, Object> entry : map.entrySet()){
for (Map.Entry<String, Object> entry2 : ((Map<String, Object>)(Object) entry).entrySet()){
sheet.addCell(new Label(c++, r+1, (String) entry2.getValue()));
}
}
以下是我的堆栈跟踪:
java.lang.ClassCastException: java.util.HashMap$Node cannot be cast to java.util.Map
at com.vz.stamps.services.TestServiceForOrderPopup.addExcelOutputHeader(TestServiceForOrderPopup.java:97)
at com.vz.stamps.services.TestServiceForOrderPopup.createExcelOutputExcel(TestServiceForOrderPopup.java:56)
at com.vz.stamps.controller.OrderviewOpenDriverController.downloadExcelOutputExl(OrderviewOpenDriverController.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
答案 0 :(得分:1)
您会收到错误消息,因为在创建第一个地图的EntrySet
并将其传递给另一个迭代之后,您没有在getValue()
上调用EntrySet
。因此,代码应更新为:
for(Map.Entry<String, Object> entry : map.entrySet()){
for (Map.Entry<String, Object> entry2 : ((Map<String, Object>)(Object) entry.getValue()).entrySet()){
sheet.addCell(new Label(c++, r+1, (String) entry2.getValue()));
}
}