```c
//JDK1.5
Map map = new HashMap();
for(Object obj : map.keySet()) {
Object key = obj;
Object value = map.get(obj);
}
```
```c
//JDK1.4
Map map = new HashMap() ;
Iterator iter = map.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
```
第一种:
```c
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
```
效率高,以后要使用此种方式!
第二种:
```c
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
```
效率低,以后尽量少使用!
标签:Map,遍历,JAVA,map,Object,iter,key,entry From: https://www.cnblogs.com/biubiu111/p/16858429.html