【发布时间】:2017-06-21 12:31:18
【问题描述】:
这是我的代码。我正在将地图插入列表中。但是当我直接将地图添加到表格中时。它显示错误。
import java.util.*;
class mapIn{
public static void main(String... a){
List<Map<Integer, String>> mapList = new ArrayList<Map<Integer,String>>();
mapList.add(new HashMap<Integer,String>().put(1,"Ram"));
mapList.add(new HashMap<Integer,String>().put(2,"Shyam"));
mapList.add(new HashMap<Integer,String>().put(3,"Shyam"));
for(Map m:mapList){
// for(Map.Entry e:m.entrySet()){
// System.out.println(e.getKey()+" "+e.getValue());
// }
Set set=m.entrySet();//Converting to Set so that we can traverse
Iterator itr=set.iterator();
while(itr.hasNext()){
//Converting to Map.Entry so that we can get key and value separately
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
}
【问题讨论】:
-
错误是什么?你能格式化你的代码吗?请阅读我们的How to Ask 页面以获取有关如何改进此问题的提示
-
返回:与 key 关联的前一个值,如果没有 key 映射,则返回 null。 (如果实现支持 null 值,则 null 返回还可以指示映射先前将 null 与 key 关联。)这是 put 返回值描述。这根本不应该编译。
标签: java list dictionary arraylist hashmap