list转换为map,急急急!

问题描述:

List> list,怎么把这个list转换为Map或者HashMap?求代码

Map 是键值对,你转换成Map 需要自己添加key,比如你的list是这样的List> list

可以这样转换
Map> listtoMap = new HashMap();
for(Map map : list){
listtoMap.put("这里你的key",map);
}

自己遍历添加到map,list中没有Key,你需要自己生成key

public Map getMap(List> list){
Map map =new HashMap();
for (int i = 0; i < list.size(); i++) {
Map smap = list.get(i);
map.putAll(smap);
}
return map;
}