在 Java 中,有多种遍历 Map
的方式。最常用的遍历方法包括使用 for-each
循环、Iterator
迭代器和 Stream API
。以下是几种常见的遍历 Map
的方式:
1. 使用 for-each
循环遍历 Map.Entry
这是最常用的方式,使用 for-each
循环遍历 Map
的 entrySet()
。
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
2. 使用 for-each
循环遍历 keySet
使用 for-each
循环遍历 Map
的 keySet()
,然后通过 get
方法获取值。
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for (String key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key: " + key + ", Value: " + value);
}
}
}
3. 使用 for-each
循环遍历 values
使用 for-each
循环遍历 Map
的 values()
,如果只需要访问值而不需要访问键。
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for (Integer value : map.values()) {
System.out.println("Value: " + value);
}
}
}
4. 使用 Iterator
遍历 Map.Entry
使用 Iterator
遍历 Map
的 entrySet()
,适用于需要在遍历过程中进行删除操作的情况。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
5. 使用 Stream API
遍历 Map
在 Java 8 及以上版本中,可以使用 Stream API
遍历 Map
。
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
// 或者使用 stream
map.entrySet().stream()
.forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
}
}
6. 使用 Streams API
进行高级操作
Stream API
不仅可以用于遍历,还可以进行过滤、映射等操作。
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
// 过滤值大于1的条目
Map<String, Integer> filteredMap = map.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
filteredMap.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
}
}
总结
以上是几种常用的遍历 Map
的方法,选择哪种方式取决于具体的需求和代码风格。在需要进行删除操作时,使用 Iterator
会更合适;在进行复杂的流式处理时,Stream API
是一个强大的工具。