Java中Map集合体系的基本使用和常用API是什么

发布时间:2023-02-01 10:47:30 作者:iii
来源:亿速云 阅读:114

Java中Map集合体系的基本使用和常用API是什么

目录

  1. Map集合概述
  2. Map接口的常用实现类
  3. Map接口的常用API
  4. Map集合的遍历
  5. Map集合的性能比较
  6. 总结

Map集合概述

在Java中,Map是一种用于存储键值对(key-value pairs)的集合。Map接口是Java集合框架的一部分,它提供了一种将键映射到值的方式。每个键最多只能映射到一个值,且键不能重复。Map接口的主要实现类包括HashMapLinkedHashMapTreeMapHashtableConcurrentHashMap

Map接口的主要特点包括: - 键值对存储:Map存储的是键值对,键和值都可以是任意类型的对象。 - 键唯一:Map中的键是唯一的,不能重复。 - 值可以重复:Map中的值可以重复,多个键可以映射到同一个值。 - 无序性:Map中的键值对是无序的,除非使用LinkedHashMapTreeMap等有序实现类。

Map接口的常用实现类

HashMap

HashMapMap接口最常用的实现类之一。它基于哈希表实现,允许存储null键和null值。HashMap不保证元素的顺序,且不保证顺序随着时间的推移保持不变。

特点:

示例代码:

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

LinkedHashMap

LinkedHashMapHashMap的子类,它在HashMap的基础上维护了一个双向链表,用于记录插入顺序或访问顺序。因此,LinkedHashMap可以保持元素的插入顺序或访问顺序。

特点:

示例代码:

Map<String, Integer> map = new LinkedHashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}
// 输出:
// apple: 1
// banana: 2
// cherry: 3

TreeMap

TreeMap是基于红黑树实现的Map接口的有序实现类。它根据键的自然顺序或自定义比较器对键进行排序。TreeMap不允许null键,但允许null值。

特点:

示例代码:

Map<String, Integer> map = new TreeMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}
// 输出:
// apple: 1
// banana: 2
// cherry: 3

Hashtable

HashtableMap接口的早期实现类,它与HashMap类似,但Hashtable是线程安全的。Hashtable不允许null键和null值。

特点:

示例代码:

Map<String, Integer> map = new Hashtable<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

System.out.println(map.get("banana")); // 输出: 2

ConcurrentHashMap

ConcurrentHashMapMap接口的线程安全实现类,它在HashMap的基础上进行了优化,支持高并发的读写操作。ConcurrentHashMap允许null值,但不允许null键。

特点:

示例代码:

Map<String, Integer> map = new ConcurrentHashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

System.out.println(map.get("banana")); // 输出: 2

Map接口的常用API

基本操作

1. put(K key, V value)

将指定的键值对插入到Map中。如果键已经存在,则替换对应的值。

Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

2. get(Object key)

返回指定键所映射的值,如果Map中不包含该键,则返回null

Integer value = map.get("banana"); // 返回: 2

3. remove(Object key)

移除指定键所映射的键值对,并返回对应的值。如果Map中不包含该键,则返回null

Integer removedValue = map.remove("banana"); // 返回: 2

4. containsKey(Object key)

判断Map中是否包含指定的键。

boolean contains = map.containsKey("apple"); // 返回: true

5. containsValue(Object value)

判断Map中是否包含指定的值。

boolean contains = map.containsValue(3); // 返回: true

6. size()

返回Map中键值对的数量。

int size = map.size(); // 返回: 3

7. isEmpty()

判断Map是否为空。

boolean isEmpty = map.isEmpty(); // 返回: false

8. clear()

清空Map中的所有键值对。

map.clear();

视图操作

1. keySet()

返回Map中所有键的集合。

Set<String> keys = map.keySet();
for (String key : keys) {
    System.out.println(key);
}

2. values()

返回Map中所有值的集合。

Collection<Integer> values = map.values();
for (Integer value : values) {
    System.out.println(value);
}

3. entrySet()

返回Map中所有键值对的集合。

Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

默认方法

1. getOrDefault(Object key, V defaultValue)

返回指定键所映射的值,如果Map中不包含该键,则返回默认值。

Integer value = map.getOrDefault("banana", 0); // 返回: 2
Integer defaultValue = map.getOrDefault("grape", 0); // 返回: 0

2. putIfAbsent(K key, V value)

如果指定键尚未与值关联(或映射到null),则将其与给定值关联并返回null,否则返回当前值。

Integer oldValue = map.putIfAbsent("banana", 4); // 返回: 2
Integer newValue = map.putIfAbsent("grape", 4); // 返回: null

3. replace(K key, V value)

替换指定键所映射的值,并返回旧值。如果Map中不包含该键,则返回null

Integer oldValue = map.replace("banana", 5); // 返回: 2

4. replace(K key, V oldValue, V newValue)

仅当指定键当前映射到指定值时,才替换该键的值。

boolean replaced = map.replace("banana", 5, 6); // 返回: true

5. forEach(BiConsumer<? super K, ? super V> action)

Map中的每个键值对执行给定的操作。

map.forEach((key, value) -> System.out.println(key + ": " + value));

Map集合的遍历

使用keySet()遍历

通过keySet()方法获取Map中所有键的集合,然后遍历键集合并获取对应的值。

for (String key : map.keySet()) {
    System.out.println(key + ": " + map.get(key));
}

使用entrySet()遍历

通过entrySet()方法获取Map中所有键值对的集合,然后遍历键值对集合。

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

使用forEach()遍历

使用forEach()方法遍历Map中的每个键值对。

map.forEach((key, value) -> System.out.println(key + ": " + value));

Map集合的性能比较

实现类 数据结构 线程安全 允许null键/值 时间复杂度(查找/插入/删除) 顺序性
HashMap 哈希表 是/是 O(1) 无序
LinkedHashMap 哈希表+双向链表 是/是 O(1) 插入或访问顺序
TreeMap 红黑树 否/是 O(log n) 自然或自定义顺序
Hashtable 哈希表 否/否 O(1) 无序
ConcurrentHashMap 分段锁 否/是 O(1) 无序

总结

Map是Java集合框架中非常重要的一部分,它提供了键值对的存储和操作功能。Map接口的常用实现类包括HashMapLinkedHashMapTreeMapHashtableConcurrentHashMap,每种实现类都有其特定的使用场景和性能特点。在实际开发中,应根据具体需求选择合适的Map实现类,并熟练掌握Map接口的常用API和遍历方式。

推荐阅读:
  1. 怎么用Java实现redis连接池
  2. 那些年,我们见过的 Java 服务端“问题”

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java map api

上一篇:PHPSTORM有哪些常用快捷键

下一篇:ntuser.dat文件有什么作用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》