**
Java的Map集合遍历
方法1:通过键获取值,进行键和值的遍历
Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println("key = " + key + ", value = " + value);
}
方法2:利用Map集合的entrySet()方法进行遍历
Map<String, String> map = new HashMap<String, String>();
for (Entry<String, String> entry : map.entrySet()) {
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
方法3:利用迭代器Iterator进行遍历
Map<String, String> map = new HashMap<String, String>();
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
方法4:利用keySet()和values()分别遍历键和值
Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
System.out.println("key = " + key);
}
for (String value : map.values()) {
System.out.println("value = " + value);
}