【发布时间】:2015-02-05 18:49:03
【问题描述】:
如何将 hashmap 值传递给 List 条目是字符串键值类型?
页面类:
public List<Entry> entry;
public void setEntry(final List<Entry> entry) {
this.entry = entry;
this.setInfo(entry);
}
public void setInfo(final List<Entry> entryList) {
this.prop = new HashMap<String, String>();
for (Entry objEntry : this.entry) {
this.prop.put(objEntry.getKey(), objEntry.getValue());
}
}
我已经创建了一张地图:
Map<String, String> info = new HashMap<String, String>();
info.put("abc", "123")
我试过了:
List l = new ArrayList(info.values());
page.setInfo(list); // I am getting class cast exception
【问题讨论】:
-
您还会收到有关未正确使用泛型的警告,这正是导致此类异常的原因。
-
这就是为什么不使用原始类型...如果您不使用原始类型,这甚至不会编译。
-
在Page类中,setInfo方法:增强for循环尝试将this.entry替换为entryList