Map 接口的核心 API

一、Map 的核心 API

1. 添加元素

  • V put(K key, V value):将指定的键值对添加到映射中。如果键已存在,则替换旧值并返回旧值;否则返回 null

  • void putAll(Map<? extends K, ? extends V> m):将指定映射中的所有键值对添加到当前映射中。

示例

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
System.out.println(map); // 输出: {Apple=1, Banana=2, Cherry=3}

Map<String, Integer> anotherMap = new HashMap<>();
anotherMap.put("Orange", 4);
anotherMap.put("Mango", 5);
map.putAll(anotherMap);
System.out.println(map); // 输出: {Apple=1, Banana=2, Cherry=3, Orange=4, Mango=5}

2. 删除元素

  • V remove(Object key):移除指定键对应的键值对,并返回对应的值;如果键不存在,则返回 null

  • boolean remove(Object key, Object value):仅当指定键和值都匹配时,移除键值对。

  • void clear():清空映射中的所有键值对。

示例

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);

map.remove("Banana");
System.out.println(map); // 输出: {Apple=1, Cherry=3}

map.remove("Apple", 2); // 键值不匹配,不会移除
System.out.println(map); // 输出: {Apple=1, Cherry=3}

map.clear();
System.out.println(map); // 输出: {}

3. 查询元素

  • V get(Object key):返回指定键对应的值;如果键不存在,则返回 null

  • boolean containsKey(Object key):判断映射是否包含指定键。

  • boolean containsValue(Object value):判断映射是否包含指定值。

  • boolean isEmpty():判断映射是否为空。

  • int size():返回映射中的键值对数量。

示例

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);

System.out.println(map.get("Banana")); // 输出: 2
System.out.println(map.containsKey("Apple")); // 输出: true
System.out.println(map.containsValue(4)); // 输出: false
System.out.println(map.isEmpty()); // 输出: false
System.out.println(map.size()); // 输出: 3

4. 遍历元素

  • Set<K> keySet():返回映射中所有键的集合。

  • Collection<V> values():返回映射中所有值的集合。

  • Set<Map.Entry<K, V>> entrySet():返回映射中所有键值对的集合。

示例

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);

// 遍历键
for (String key : map.keySet()) {
    System.out.println("Key: " + key);
}

// 遍历值
for (Integer value : map.values()) {
    System.out.println("Value: " + value);
}

// 遍历键值对
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

5. 替换元素

  • V replace(K key, V value):替换指定键对应的值(如果键存在)。

  • boolean replace(K key, V oldValue, V newValue):仅当指定键和旧值都匹配时,替换为新值。

示例

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);

map.replace("Banana", 3);
System.out.println(map); // 输出: {Apple=1, Banana=3}

map.replace("Apple", 1, 4);
System.out.println(map); // 输出: {Apple=4, Banana=3}

6. 其他方法

  • boolean equals(Object o):判断映射是否与指定对象相等。

  • int hashCode():返回映射的哈希码值。

示例

Map<String, Integer> map1 = new HashMap<>();
map1.put("Apple", 1);
map1.put("Banana", 2);

Map<String, Integer> map2 = new HashMap<>();
map2.put("Banana", 2);
map2.put("Apple", 1);

System.out.println(map1.equals(map2)); // 输出: true
System.out.println(map1.hashCode() == map2.hashCode()); // 输出: true

二、Map 的实现类

1. HashMap

  • 特点

    • 基于哈希表实现,键值对无序。

    • 允许 null 键和 null 值。

    • 插入、删除和查找操作的时间复杂度为 O(1)。

  • 示例

    Map<String, Integer> hashMap = new HashMap<>();
    hashMap.put("Apple", 1);
    hashMap.put("Banana", 2);
    System.out.println(hashMap); // 输出: {Apple=1, Banana=2}

2. LinkedHashMap

  • 特点

    • 基于哈希表和链表实现,键值对按插入顺序排序。

    • 允许 null 键和 null 值。

    • 插入、删除和查找操作的时间复杂度为 O(1)。

  • 示例

    Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
    linkedHashMap.put("Apple", 1);
    linkedHashMap.put("Banana", 2);
    linkedHashMap.put("Cherry", 3);
    System.out.println(linkedHashMap); // 输出: {Apple=1, Banana=2, Cherry=3}

3. TreeMap

  • 特点

    • 基于红黑树实现,键值对按自然顺序或指定比较器排序。

    • 不允许 null 键(但允许 null 值)。

    • 插入、删除和查找操作的时间复杂度为 O(log n)。

  • 示例

    Map<String, Integer> treeMap = new TreeMap<>();
    treeMap.put("Banana", 2);
    treeMap.put("Apple", 1);
    treeMap.put("Cherry", 3);
    System.out.println(treeMap); // 输出: {Apple=1, Banana=2, Cherry=3}

三、Map 的常见问题

1. 如何保证键的唯一性?

  • HashMap 和 LinkedHashMap 通过 equals() 和 hashCode() 方法判断键是否重复。

  • TreeMap 通过 compareTo() 或 Comparator 判断键是否重复。


2. 如何选择 Map 的实现类?

  • 如果需要快速查找且不关心顺序,使用 HashMap

  • 如果需要按插入顺序排序,使用 LinkedHashMap

  • 如果需要按自然顺序或自定义顺序排序,使用 TreeMap


四、总结

方法说明
put(K key, V value)添加键值对(如果键已存在,则替换旧值)。
remove(Object key)移除指定键对应的键值对。
get(Object key)返回指定键对应的值。
containsKey(Object key)判断是否包含指定键。
keySet()返回所有键的集合。
entrySet()返回所有键值对的集合。
clear()清空映射中的所有键值对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值