如何一次性遍历Map中的所有对象

来源:互联网 发布:德国经济数据 编辑:程序博客网 时间:2024/04/29 20:41


public class MapDemo {


/**
* @param 如何一次性遍历Map中的所有对象,由于Map集合中间没有迭代器,所以只能使用间接的方式。也可以看出,Map集合中间Key用的是Set来存储的
*/
public static void main(String[] args) {
Map<Integer, String> hashMap=new HashMap<Integer, String>();
hashMap.put(1, "zhansan");
hashMap.put(2, "xushi");
hashMap.put(3, "hejiu");
hashMap.put(4, "zhouba");
hashMap.put(5, "zhaoliu");
hashMap.put(5, "wangwu");
hashMap.put(7, "lisi");
Set<Integer> keySet= hashMap.keySet();
Iterator it=keySet.iterator();
while(it.hasNext()){
Integer key=(Integer) it.next();
String value=hashMap.get(key);
System.out.println(value);



}

}
}
原创粉丝点击