在 Java 中,获取 Map 的 value 有多种方法,具体取决于你的需求(是否需要处理 null 值、是否需要获取所有值、是否需要单个值等),以下是几种常见的方法:

通过 key 获取单个 value
使用 Map.get(key) 方法,key 不存在,则返回 null。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
// 获取 "apple" 对应的 value
Integer value = map.get("apple"); // 返回 10
System.out.println(value); // 输出: 10
// 获取不存在的 key
Integer nonExistentValue = map.get("orange"); // 返回 null
System.out.println(nonExistentValue); // 输出: null
注意事项
value本身可能是null,可以使用containsKey(key)判断key是否存在:if (map.containsKey("apple")) { Integer value = map.get("apple"); System.out.println(value); }
获取所有 value(Collection<V>)
使用 Map.values() 方法返回一个 Collection<V>,包含所有 value。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
Collection<Integer> values = map.values();
System.out.println(values); // 输出: [10, 20](顺序可能不同)
// 遍历所有 value
for (Integer value : values) {
System.out.println(value);
}
特点
- 返回的
Collection是Map的视图,修改Map会影响Collection,反之亦然(除非使用new ArrayList<>(map.values())复制一份)。
使用 Map.getOrDefault(key, defaultValue)
key 不存在,返回默认值,避免 NullPointerException。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
int value = map.getOrDefault("apple", 0); // 返回 10
int nonExistentValue = map.getOrDefault("orange", 0); // 返回 0
System.out.println(value); // 输出: 10
System.out.println(nonExistentValue); // 输出: 0
使用 Map.computeIfAbsent(key, function)
key 不存在,则计算并存储 value,然后返回 value。

Map<String, Integer> map = new HashMap<>();
// "apple" 不存在,则计算并存储其 value(这里用 lambda 表达式)
int value = map.computeIfAbsent("apple", k -> 10); // 返回 10
System.out.println(value); // 输出: 10
System.out.println(map); // 输出: {apple=10}
// "banana" 已存在,则直接返回原 value
map.put("banana", 20);
int bananaValue = map.computeIfAbsent("banana", k -> 30); // 返回 20
System.out.println(bananaValue); // 输出: 20
使用 Map.forEach() 遍历所有 value
Java 8+ 提供了 forEach 方法,可以遍历 Map 并处理 value。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.forEach((key, value) -> {
System.out.println("Key: " + key + ", Value: " + value);
});
输出:
Key: apple, Value: 10
Key: banana, Value: 20
使用 Stream 处理 value(Java 8+)
如果需要对 value 进行过滤、转换等操作,可以使用 Stream。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 5);
// 获取所有大于 10 的 value
map.values().stream()
.filter(value -> value > 10)
.forEach(System.out::println); // 输出: 20
// 转换为 List<Integer>
List<Integer> valueList = map.values().stream()
.collect(Collectors.toList());
System.out.println(valueList); // 输出: [10, 20, 5]
| 方法 | 适用场景 | 返回类型 | 特点 |
|---|---|---|---|
map.get(key) |
获取单个 value |
V |
key 不存在时返回 null |
map.values() |
获取所有 value |
Collection<V> |
返回视图,非独立副本 |
map.getOrDefault(key, defaultValue) |
安全获取单个 value |
V |
避免 NullPointerException |
map.computeIfAbsent(key, function) |
动态计算并获取 value |
V |
适用于懒加载计算 |
map.forEach() |
遍历所有 key-value |
void |
适用于 Java 8+ |
map.values().stream() |
流式处理 value |
Stream<V> |
适用于 Java 8+ |
根据你的需求选择合适的方法!

