1.利用keyset進行遍歷,根據key值獲取value:
public static void workByKeySet(Map<String, String> map) {
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
System.out.println(map.get(s));
}
}
2.同時得到key和value:
public static void workByEntry(Map<String, String> map) {
Set<Map.Entry<String, Student>> set = map.entrySet();
for (Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}