在Java中,可以使用以下幾種方法來遍歷HashMap:
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
for (Integer value : map.values()) {
System.out.println("Value: " + value);
}