溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2023-02-01 10:47:30 來源:億速云 閱讀:116 作者:iii 欄目:編程語言

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

目錄

  1. Map集合概述
  2. Map接口的常用實現類
  3. Map接口的常用API
  4. Map集合的遍歷
  5. Map集合的性能比較
  6. 總結

Map集合概述

在Java中,Map是一種用于存儲鍵值對(key-value pairs)的集合。Map接口是Java集合框架的一部分,它提供了一種將鍵映射到值的方式。每個鍵最多只能映射到一個值,且鍵不能重復。Map接口的主要實現類包括HashMap、LinkedHashMap、TreeMap、HashtableConcurrentHashMap。

Map接口的主要特點包括: - 鍵值對存儲:Map存儲的是鍵值對,鍵和值都可以是任意類型的對象。 - 鍵唯一:Map中的鍵是唯一的,不能重復。 - 值可以重復:Map中的值可以重復,多個鍵可以映射到同一個值。 - 無序性:Map中的鍵值對是無序的,除非使用LinkedHashMapTreeMap等有序實現類。

Map接口的常用實現類

HashMap

HashMapMap接口最常用的實現類之一。它基于哈希表實現,允許存儲null鍵和null值。HashMap不保證元素的順序,且不保證順序隨著時間的推移保持不變。

特點:

  • 基于哈希表實現,查找、插入和刪除操作的時間復雜度為O(1)。
  • 允許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

LinkedHashMapHashMap的子類,它在HashMap的基礎上維護了一個雙向鏈表,用于記錄插入順序或訪問順序。因此,LinkedHashMap可以保持元素的插入順序或訪問順序。

特點:

  • 基于哈希表和雙向鏈表實現,查找、插入和刪除操作的時間復雜度為O(1)。
  • 保持插入順序或訪問順序。
  • 允許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

TreeMap是基于紅黑樹實現的Map接口的有序實現類。它根據鍵的自然順序或自定義比較器對鍵進行排序。TreeMap不允許null鍵,但允許null值。

特點:

  • 基于紅黑樹實現,查找、插入和刪除操作的時間復雜度為O(log n)。
  • 鍵按照自然順序或自定義比較器排序。
  • 不允許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值。

特點:

  • 基于哈希表實現,查找、插入和刪除操作的時間復雜度為O(1)。
  • 線程安全。
  • 不允許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鍵。

特點:

  • 基于分段鎖實現,支持高并發。
  • 查找、插入和刪除操作的時間復雜度為O(1)。
  • 允許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接口的常用實現類包括HashMap、LinkedHashMap、TreeMap、HashtableConcurrentHashMap,每種實現類都有其特定的使用場景和性能特點。在實際開發中,應根據具體需求選擇合適的Map實現類,并熟練掌握Map接口的常用API和遍歷方式。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女