在 JDK 8 中,Map
接口引入了许多新的方法,这些方法使得对 Map
的操作更加简便和高效。以下是一些常用方法的介绍和代码示例。
1. getOrDefault(K key, V defaultValue)
getOrDefault
方法用于根据给定的键获取映射中的值。如果键不存在,则返回指定的默认值,而不是返回 null
。
示例:
import java.util.HashMap;
import java.util.Map;
public class GetOrDefaultExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
// 获取已存在的键
System.out.println(map.getOrDefault("apple", 0)); // 输出 3
// 获取不存在的键,返回默认值
System.out.println(map.getOrDefault("orange", 0)); // 输出 0
}
}
2. forEach(BiConsumer<? super K, ? super V> action)
forEach
方法用于对 Map
中的每个键值对执行指定的操作。此方法通常用于遍历 Map
。
示例:
import java.util.HashMap;
import java.util.Map;
public class ForEachExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
// 遍历 Map
map.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
3. merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
merge
方法尝试将给定的值与现有值进行合并。如果指定的键已经存在,使用 remappingFunction
合并旧值和新值。如果键不存在,则直接将值放入 Map
中。
示例:
import java.util.HashMap;
import java.util.Map;
public class MergeExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
// 如果键已存在,合并值
map.merge("apple", 2, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map.get("apple")); // 输出 5 (3 + 2)
// 如果键不存在,直接添加新值
map.merge("orange", 4, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map.get("orange")); // 输出 4
}
}
4. putIfAbsent(K key, V value)
putIfAbsent
方法用于将指定的键值对放入 Map
中,只有在键不存在时才会插入。如果键已存在,方法不做任何操作。
示例:
import java.util.HashMap;
import java.util.Map;
public class PutIfAbsentExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// 只有在 "banana" 不存在时才添加
map.putIfAbsent("banana", 5);
map.putIfAbsent("apple", 10); // "apple" 已存在,不会更新
System.out.println(map);
}
}
5. compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
compute
方法根据给定的 key
和当前的 value
调用指定的 remappingFunction
,并将计算结果放入 Map
中。如果键不存在,则可以通过 remappingFunction
计算出新值并添加到 Map
中。
示例:
import java.util.HashMap;
import java.util.Map;
public class ComputeExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// 使用计算函数更新键 "apple" 的值
map.compute("apple", (key, value) -> value == null ? 1 : value + 2);
System.out.println(map.get("apple")); // 输出 5
// 键 "banana" 不存在,调用 remappingFunction 后会插入新值
map.compute("banana", (key, value) -> 4);
System.out.println(map.get("banana")); // 输出 4
}
}
6. computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
computeIfAbsent
方法只有在 Map
中不存在给定的 key
时才会使用指定的 mappingFunction
来计算值,并将其插入到 Map
中。
示例:
import java.util.HashMap;
import java.util.Map;
public class ComputeIfAbsentExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// 键 "banana" 不存在,调用 mappingFunction 生成值
map.computeIfAbsent("banana", key -> key.length());
System.out.println(map.get("banana")); // 输出 6 (banana的长度)
// 键 "apple" 已经存在,不会调用 mappingFunction
map.computeIfAbsent("apple", key -> 100);
System.out.println(map.get("apple")); // 输出 3
}
}
7. computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
computeIfPresent
方法只有在 Map
中已存在给定的 key
时,才会通过 remappingFunction
更新其对应的值。
示例:
import java.util.HashMap;
import java.util.Map;
public class ComputeIfPresentExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// 键 "apple" 存在,更新其值
map.computeIfPresent("apple", (key, value) -> value + 2);
System.out.println(map.get("apple")); // 输出 5
// 键 "banana" 不存在,不会进行任何操作
map.computeIfPresent("banana", (key, value) -> value + 1);
System.out.println(map.get("banana")); // 输出 null
}
}
8. replace(K key, V value)
replace
方法将 Map
中给定键的值替换为指定的新值。如果键不存在,方法将不做任何操作。
示例:
import java.util.HashMap;
import java.util.Map;
public class ReplaceExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
// 替换已存在键的值
map.replace("apple", 5);
System.out.println(map.get("apple")); // 输出 5
// 键 "banana" 不存在,不会进行替换
map.replace("banana", 10);
System.out.println(map.get("banana")); // 输出 null
}
}
JDK 8 中新增的这些 Map
方法大大增强了 Map
的功能,尤其是在处理键值对时更加灵活和简洁。无论是用于替代传统的条件判断,还是通过函数式编程提高代码可读性,这些方法都能够提高开发效率,减少冗余代码。