常用2种:keyset和entryset
keyset遍历:
1 Map map = new HashMap();
2 Iterator iter = map.keySet().iterator();
3 while (iter.hasNext()) {
4 Object key = iter.next();
5 Object val = map.get(key);
6 }
entryset遍历:
1 Map map = new HashMap();
2 Iterator iter = map.entrySet().iterator();
3 while (iter.hasNext()) {
4 Map.Entry entry = (Map.Entry) iter.next();
5 Object key = entry.getKey();
6 Object val = entry.getValue();
7 }
后者因为直接取出了key和value,速度上要快些。