import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TestMap {
/**
* map的遍历方法
*/
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
// map中的每条记录(key, value)是Set类型,而Set的类型是Map.Entry<K, V>,这一切通过map的entrySet()获取
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// 遍历Set
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
map的遍历方法
最新推荐文章于 2024-08-30 08:44:03 发布