//创建一个map对象并赋值
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 10; i++) {
map.put("Key" + i, "Value" + i);
}
//使用keySet便利
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println("key = " + s + ";value = " + map.get(s));
}
//使用entrySet便利
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println("key = " + entry.getKey() + ";value = " + entry.getValue());
}
//使用iterator便利
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("key = " + entry.getKey() + ";value = " + entry.getValue());
}
//便利所有的value
for (String value : map.values()) {
System.out.println("value = " + value);
}
//遍历所有的key
for (String s : keySet) {
System.out.println("key = " + s);
}