1.通过Map.KeySet遍历key和value
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println("key: " + key + " value: " + value);
}
1
2
3
4
2.通过Map.entrySet使用iterator遍历key和value
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());
}
1
2
3
4
5
3.通过Map.entrySet遍历key和value
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
1
2
3
4.通过Map.values()遍历value,无法遍历key
for (String value : map.values()) {
System.out.println(" value: " + value);
}
1
2
3
5.通过Map.keySet()key,无法遍历value
for (String key : map.keySet()) {
System.out.println(" key: " + key);
}
---------------------
原文:https://blog.csdn.net/yf_man/article/details/78183082