我有一个LinkedHashMap
,它包含字符串键和ArrayList
值。当我尝试迭代它时,我得到一个异常,说明"java.lang.String不能强制转换为java.util.ArrayList"代码:
Iterator it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String key = (String)entry.getKey();
ArrayList maps = (ArrayList)entry.getValue();
}
如何在没有异常的情况下进行迭代?
keySet()的工作速度会更快吗?这可能更准确,但在我的脑海中。。。
Iterator it = hashMap.keySet().iterator();
while (it.hasNext()) {
String key = (String)it.next();
ArrayList maps = (ArrayList)hashMap.get(key);
}