import java.util.*;
/**
* MAP 遍历测试
* @author : ZhouMei
* @date Date : 2022年08月11日 11:23
* @Description: TODO
*/
public class MapIteratorTest {
public static void main(String[] args) {
//Map map = new HashMap();
Map map = new LinkedHashMap();
map.put("2", "2222");
map.put("4", "4444");
map.put("1", "1111");
map.put("3", "3333");
//方法一
Set keySet = map.keySet();
Iterator iterator = keySet.iterator();
while (iterator.hasNext()){
Object key = iterator.next();
Object value = map.get(key);
System.out.println(key + ": " + value);
}
//方法二
Set entrySet = map.entrySet();
Iterator it2 = entrySet.iterator();
while (it2.hasNext()){
Map.Entry obj = (Map.Entry)it2.next();
Object key = obj.getKey();
Object value = obj.getValue();
System.out.println(key + ": " + value);
}
}
}
- 测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d2fa07f94d4101ab55555c0f6c1834c6.png)