方法1:使用Iterator,,适合在遍历过程中删除元素推荐使用Iterator;
more >>1
2
3
4
5
6
7Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 2);
Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}