java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index
上述错误是在做一个趋势投资demo时遇到的.
说的是链式HashMap不能转换为单个对象进行foreach
其解决:
private List<IndexData> map2IndexData(List<Map> temp) { List<IndexData> indexDatas = new ArrayList<>(); for (Map map : temp) { String date = map.get("date").toString(); float closePoint = Convert.toFloat(map.get("closePoint")); IndexData indexData = new IndexData(); indexData.setDate(date); indexData.setClosePoint(closePoint); indexDatas.add(indexData); } return indexDatas; }
private List<Index> map2Index(List<Map> temp) { List<Index> indexes = new ArrayList<>(); for (Map map : temp) { String code = map.get("code").toString(); String name = map.get("name").toString(); Index index= new Index(); index.setCode(code); index.setName(name); indexes.add(index); } return indexes; }