252. Java 集合 - 管理 Map 的内容(续)
1. 🗑️ 从 Map 中移除键值对
在 Map 中移除元素,主要使用两种 remove() 方法:
✨ 基础版:remove(key)
- 直接根据键移除对应的键值对。
- 返回被移除的值;如果键不存在,返回
null。
📖 示例:
import java.util.HashMap;
import java.util.Map;
public class RemoveExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Integer removedValue = map.remove("one");
System.out.println("Removed value: " + removedValue); // 1
System.out.println("Map after removal: " + map); // {two=2}
}
}
✨ 更安全版:remove(key, value)
Java 8 新增了重载方法:
- 只有当键和值都匹配时,才真正移除。
- 返回
true表示成功移除,false表示未移除(键不存在或值不匹配)。
📖 示例:
public class SafeRemoveExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 100);
map.put("banana", 200);
boolean result1 = map.remove("apple", 100); // 正确匹配
boolean result2 = map.remove("banana", 300); // 键存在但值不匹配
System.out.println("Removed apple? " + result1); // true
System.out.println("Removed banana? " + result2); // false
System.out.println("Map after removal: " + map); // {banana=200}
}
}
✅ 总结:
remove(key)—— 根据键移除,不关心值。remove(key, value)—— 键和值必须都匹配才移除,更安全!
2. 🔎 检查 Map 中是否存在某个键或值
可以使用以下两个方法:
| 方法 | 说明 |
|---|---|
containsKey(key) | 检查指定键是否存在 |
containsValue(value) | 检查指定值是否存在 |
返回结果都是 true 或 false。
📖 示例:
public class ContainsExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("cat", 1);
map.put("dog", 2);
System.out.println(map.containsKey("cat")); // true
System.out.println(map.containsKey("bird")); // false
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // false
}
}
3. 📦 检查 Map 的内容状态
Map 还提供了一些类似集合(Collection)的基本操作:
| 方法 | 说明 |
|---|---|
isEmpty() | 检查 Map 是否为空,空返回 true |
size() | 获取 Map 中键值对的数量 |
clear() | 清空 Map 中的所有键值对 |
putAll(otherMap) | 将另一个 Map 中的所有内容合并到当前 Map 中 |
📖 示例:基本操作
public class MapStateExample {
public static void main(String[] args) {
Map<String, Integer> map1 = new HashMap<>();
map1.put("A", 10);
map1.put("B", 20);
System.out.println("Is map1 empty? " + map1.isEmpty()); // false
System.out.println("Size of map1: " + map1.size()); // 2
Map<String, Integer> map2 = new HashMap<>();
map2.put("B", 200);
map2.put("C", 300);
map1.putAll(map2); // 合并 map2 到 map1
System.out.println("Map1 after putAll: " + map1); // {A=10, B=200, C=300}
map1.clear();
System.out.println("Map1 after clear: " + map1); // {}
}
}
✅ 注意:
putAll()中如果两个 Map 有相同的键,后者的值覆盖前者!clear()后,Map 完全为空,size()变为 0。
🎯 小结
| 操作 | 方法 | 返回类型 | 特点 |
|---|---|---|---|
| 移除键值对(根据键) | remove(key) | 值或 null | 直接按键删除 |
| 移除键值对(键值都匹配) | remove(key, value) | boolean | 更安全的移除方式 |
| 检查键是否存在 | containsKey(key) | boolean | 检查某个键 |
| 检查值是否存在 | containsValue(value) | boolean | 检查某个值 |
| 检查是否为空 | isEmpty() | boolean | 空返回 true |
| 获取大小 | size() | int | 返回键值对的数量 |
| 合并另一个 Map | putAll(otherMap) | void | 后覆盖前 |
| 清空 Map | clear() | void | 清空所有数据 |
👀 互动问题
问题1:如果我只知道键,不确定值,应该用哪个 remove() 方法?
答案示例:用
remove(key);如果想更安全,可以用remove(key, value)。
问题2:putAll() 方法中遇到键冲突,哪边的值会保留?
答案示例:新 Map(参数传入的)中的值会覆盖旧 Map 中的值。
问题3:如果调用 clear() 后,isEmpty() 返回什么?
答案示例:返回
true,表示 Map 已经清空。