Map的遍历有两种方式,个人感觉第二种用起来更方便一些:
@Test
public void test2(){
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zhangsan", 1000);
map.put("lisi", 2000);
map.put("wangwu", 3000);
//方式一
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for(Map.Entry<String, Integer> m:entrySet){
System.out.println(m.getKey()+","+m.getValue());
}
System.out.println("-----------------");
//方式二
Set<String> keys = map.keySet();
for(String key:keys){
System.out.println(map.get(key));
}
}