1. 创建Map
-
HashMap: 最常用的实现,基于哈希表,提供O(1)的平均时间复杂度。
Map<Integer, String> map = new HashMap<>();
-
TreeMap: 基于红黑树,键按自然顺序或自定义顺序排序,提供O(log n)的时间复杂度。
Map<Integer, String> map = new TreeMap<>();
2. 添加元素
-
put(K key, V value): 添加键值对,若键已存在则更新值。
map.put(1, "Apple"); map.put(2, "Banana");
3. 获取元素
-
get(Object key): 根据键获取值,若键不存在返回
null
。String value = map.get(1); // "Apple"
-
getOrDefault(Object key, V defaultValue): 键存在时返回值,否则返回默认值。
String value = map.getOrDefault(3, "Unknown"); // "Unknown"
4. 删除元素
-
remove(Object key): 删除指定键的键值对。
map.remove(1);
5. 检查元素
-
containsKey(Object key): 检查是否包含指定键。
boolean contains = map.containsKey(1); // false
-
containsValue(Object value): 检查是否包含指定值。
boolean contains = map.containsValue("Banana"); // true
6. 遍历Map
-
keySet(): 获取所有键的集合。
for (Integer key : map.keySet()) { System.out.println(key); }
-
values(): 获取所有值的集合。
for (String value : map.values()) { System.out.println(value); }
-
entrySet(): 获取所有键值对的集合。
for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); }
7. 其他常用操作
-
size(): 返回Map中键值对的数量。
int size = map.size();
-
isEmpty(): 检查Map是否为空。
boolean isEmpty = map.isEmpty();
-
clear(): 清空Map。
map.clear();
8. 排序
-
TreeMap: 自动按键排序。
Map<Integer, String> sortedMap = new TreeMap<>(map);
-
自定义排序: 使用
Comparator
进行自定义排序。Map<Integer, String> sortedMap = new TreeMap<>(Comparator.reverseOrder()); sortedMap.putAll(map);
9. 合并Map
-
putAll(Map<? extends K, ? extends V> m): 将另一个Map的所有键值对添加到当前Map。
Map<Integer, String> anotherMap = new HashMap<>(); anotherMap.put(3, "Cherry"); map.putAll(anotherMap);
10. 统计频率
-
使用Map统计元素频率:
int[] nums = {1, 2, 2, 3, 3, 3}; Map<Integer, Integer> frequencyMap = new HashMap<>(); for (int num : nums) { frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); }
computeIfAbsent | map.computeIfAbsent(key, k -> new V()) | 如果键不存在,则初始化值 |
Map<Character, List<Integer>> charPositions = new HashMap<>();
String word = "hello";
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
charPositions.computeIfAbsent(c, k -> new ArrayList<>()).add(i);
}
// 输出:{h=[0], e=[1], l=[2, 3], o=[4]}
getOrDefault | map.getOrDefault(key, defaultValue) | 获取值,若键不存在则返回默认值 |
int[] nums = {1, 2, 2, 3, 3, 3};
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : nums) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
// 输出:{1=1, 2=2, 3=3}
遍历键值对 | for (Map.Entry<K, V> entry : map.entrySet()) | 遍历 Map 的所有键值对 |
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
// 输出:apple -> 1, banana -> 2
遍历键 | for (K key : map.keySet()) | 遍历 Map 的所有键 |
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
for (String key : map.keySet()) {
System.out.println(key);
}
// 输出:apple, banana
containsKey | map.containsKey(key) | 检查 Map 是否包含指定键 |
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
boolean contains = map.containsKey("apple"); // true
boolean contains2 = map.containsKey("banana"); // false