HashMap的四种遍历方式
public class Test {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
for (String data : map.keySet()) {
System.out.println("key:" + data);
System.out.println("value:" + map.get(data));
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
for (Integer data : map.values()) {
System.out.println(data);
}
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
}
}