您好,登录后才能下订单哦!
在Java中,Map
是一种用于存储键值对(key-value pairs)的集合。Map
接口是Java集合框架的一部分,它提供了一种将键映射到值的方式。每个键最多只能映射到一个值,且键不能重复。Map
接口的主要实现类包括HashMap
、LinkedHashMap
、TreeMap
、Hashtable
和ConcurrentHashMap
。
Map
接口的主要特点包括:
- 键值对存储:Map
存储的是键值对,键和值都可以是任意类型的对象。
- 键唯一:Map
中的键是唯一的,不能重复。
- 值可以重复:Map
中的值可以重复,多个键可以映射到同一个值。
- 无序性:Map
中的键值对是无序的,除非使用LinkedHashMap
或TreeMap
等有序实现类。
HashMap
是Map
接口最常用的实现类之一。它基于哈希表实现,允许存储null
键和null
值。HashMap
不保证元素的顺序,且不保证顺序随着时间的推移保持不变。
null
键和null
值。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
是HashMap
的子类,它在HashMap
的基础上维护了一个双向链表,用于记录插入顺序或访问顺序。因此,LinkedHashMap
可以保持元素的插入顺序或访问顺序。
null
键和null
值。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
是基于红黑树实现的Map
接口的有序实现类。它根据键的自然顺序或自定义比较器对键进行排序。TreeMap
不允许null
键,但允许null
值。
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
是Map
接口的早期实现类,它与HashMap
类似,但Hashtable
是线程安全的。Hashtable
不允许null
键和null
值。
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
是Map
接口的线程安全实现类,它在HashMap
的基础上进行了优化,支持高并发的读写操作。ConcurrentHashMap
允许null
值,但不允许null
键。
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
put(K key, V value)
将指定的键值对插入到Map
中。如果键已经存在,则替换对应的值。
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
get(Object key)
返回指定键所映射的值,如果Map
中不包含该键,则返回null
。
Integer value = map.get("banana"); // 返回: 2
remove(Object key)
移除指定键所映射的键值对,并返回对应的值。如果Map
中不包含该键,则返回null
。
Integer removedValue = map.remove("banana"); // 返回: 2
containsKey(Object key)
判断Map
中是否包含指定的键。
boolean contains = map.containsKey("apple"); // 返回: true
containsValue(Object value)
判断Map
中是否包含指定的值。
boolean contains = map.containsValue(3); // 返回: true
size()
返回Map
中键值对的数量。
int size = map.size(); // 返回: 3
isEmpty()
判断Map
是否为空。
boolean isEmpty = map.isEmpty(); // 返回: false
clear()
清空Map
中的所有键值对。
map.clear();
keySet()
返回Map
中所有键的集合。
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
}
values()
返回Map
中所有值的集合。
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println(value);
}
entrySet()
返回Map
中所有键值对的集合。
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
getOrDefault(Object key, V defaultValue)
返回指定键所映射的值,如果Map
中不包含该键,则返回默认值。
Integer value = map.getOrDefault("banana", 0); // 返回: 2
Integer defaultValue = map.getOrDefault("grape", 0); // 返回: 0
putIfAbsent(K key, V value)
如果指定键尚未与值关联(或映射到null
),则将其与给定值关联并返回null
,否则返回当前值。
Integer oldValue = map.putIfAbsent("banana", 4); // 返回: 2
Integer newValue = map.putIfAbsent("grape", 4); // 返回: null
replace(K key, V value)
替换指定键所映射的值,并返回旧值。如果Map
中不包含该键,则返回null
。
Integer oldValue = map.replace("banana", 5); // 返回: 2
replace(K key, V oldValue, V newValue)
仅当指定键当前映射到指定值时,才替换该键的值。
boolean replaced = map.replace("banana", 5, 6); // 返回: true
forEach(BiConsumer<? super K, ? super V> action)
对Map
中的每个键值对执行给定的操作。
map.forEach((key, value) -> System.out.println(key + ": " + value));
通过keySet()
方法获取Map
中所有键的集合,然后遍历键集合并获取对应的值。
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
通过entrySet()
方法获取Map
中所有键值对的集合,然后遍历键值对集合。
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
使用forEach()
方法遍历Map
中的每个键值对。
map.forEach((key, value) -> System.out.println(key + ": " + value));
实现类 | 数据结构 | 线程安全 | 允许null键/值 | 时间复杂度(查找/插入/删除) | 顺序性 |
---|---|---|---|---|---|
HashMap | 哈希表 | 否 | 是/是 | O(1) | 无序 |
LinkedHashMap | 哈希表+双向链表 | 否 | 是/是 | O(1) | 插入或访问顺序 |
TreeMap | 红黑树 | 否 | 否/是 | O(log n) | 自然或自定义顺序 |
Hashtable | 哈希表 | 是 | 否/否 | O(1) | 无序 |
ConcurrentHashMap | 分段锁 | 是 | 否/是 | O(1) | 无序 |
Map
是Java集合框架中非常重要的一部分,它提供了键值对的存储和操作功能。Map
接口的常用实现类包括HashMap
、LinkedHashMap
、TreeMap
、Hashtable
和ConcurrentHashMap
,每种实现类都有其特定的使用场景和性能特点。在实际开发中,应根据具体需求选择合适的Map
实现类,并熟练掌握Map
接口的常用API和遍历方式。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。