Java中可以通过以下几种方式获取Map集合中的元素:
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
Set<String> keys = map.keySet();
for (String key : keys) {
Integer value = map.get(key);
System.out.println("Key: " + key + ", Value: " + value);
}
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println("Value: " + value);
}