非線性容器HashMap

2024-01-23 18:12 更新

HashMap底層使用數(shù)組+鏈表+紅黑樹的方式實現(xiàn),查詢、插入和刪除的效率都很高。HashMap存儲內(nèi)容基于key-value的鍵值對映射,不能有重復的key,且一個key只能對應一個value。

HashMap和TreeMap相比,HashMap依據(jù)鍵的hashCode存取數(shù)據(jù),訪問速度較快。而TreeMap是有序存取,效率較低。

HashSet基于HashMap實現(xiàn)。HashMap的輸入?yún)?shù)由key、value兩個值組成。在HashSet中,只對value對象進行處理。

推薦使用場景: 需要快速存取、刪除以及插入鍵值對數(shù)據(jù)時,推薦使用HashMap。

文檔中存在泛型的使用,涉及以下泛型標記符:

  • K:Key,鍵

  • V:Value,值
說明

本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

  1. import HashMap from '@ohos.util.HashMap';

HashMap

屬性

系統(tǒng)能力: SystemCapability.Utils.Lang

名稱類型可讀可寫說明
lengthnumberHashMap的元素個數(shù)。

constructor

constructor()

HashMap的構造函數(shù)。

系統(tǒng)能力: SystemCapability.Utils.Lang

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200012The HashMap's constructor cannot be directly invoked.

示例:

  1. let hashMap = new HashMap();

isEmpty

isEmpty(): boolean

判斷該HashMap是否為空。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
boolean為空返回true,不為空返回false。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The isEmpty method cannot be bound.

示例:

  1. const hashMap = new HashMap();
  2. let result = hashMap.isEmpty();

hasKey

hasKey(key: K): boolean

判斷此HashMap中是否含有該指定key。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
keyK指定Key。

返回值:

類型說明
boolean包含指定Key返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The hasKey method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. let result = hashMap.hasKey("squirrel");
  3. hashMap.set("squirrel", 123);
  4. let result1 = hashMap.hasKey("squirrel");

hasValue

hasValue(value: V): boolean

判斷此HashMap中是否含有該指定value。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
valueV指定value。

返回值:

類型說明
boolean包含指定value返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The hasValue method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. let result = hashMap.hasValue(123);
  3. hashMap.set("squirrel", 123);
  4. let result1 = hashMap.hasValue(123);

get

get(key: K): V

獲取指定key所對應的value。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
keyK查找的指定key。

返回值:

類型說明
V返回key映射的value值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The get method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let result = hashMap.get("sparrow");

setAll

setAll(map: HashMap<K, V>): void

將一個HashMap中的所有元素組添加到另一個hashMap中。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
mapHashMap<K, V>被添加元素的hashMap。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The setAll method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let newHashMap = new HashMap();
  5. hashMap.setAll(newHashMap);

set

set(key: K, value: V): Object

向HashMap中添加一組數(shù)據(jù)。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
keyK添加成員數(shù)據(jù)的鍵名。
valueV添加成員數(shù)據(jù)的值。

返回值:

類型說明
Object返回添加后的hashMap。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The set method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. let result = hashMap.set("squirrel", 123);

remove

remove(key: K): V

刪除指定key所對應元素。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
keyK指定key。

返回值:

類型說明
V返回刪除元素的值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The remove method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let result = hashMap.remove("sparrow");

clear

clear(): void

清除HashMap中的所有元素,并把length置為0。

系統(tǒng)能力: SystemCapability.Utils.Lang

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The clear method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. hashMap.clear();

keys

keys(): IterableIterator<K>

返回包含此映射中包含的鍵的新迭代器對象。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
IterableIterator<K>返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The keys method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let iter = hashMap.keys();
  5. let temp = iter.next().value;
  6. while(temp != undefined) {
  7. console.log("value:" + temp);
  8. temp = iter.next().value;
  9. }

values

values(): IterableIterator<V>

返回包含此映射中包含的鍵值的新迭代器對象。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
IterableIterator<V>返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID錯誤信息
10200011The values method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let iter = hashMap.values();
  5. let temp = iter.next().value;
  6. while(temp != undefined) {
  7. console.log("value:" + temp);
  8. temp = iter.next().value;
  9. }

replace

replace(key: K, newValue: V): boolean

對HashMap中一組數(shù)據(jù)進行更新(替換)。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
keyK依據(jù)key指定替換的元素。
newValueV替換成員數(shù)據(jù)的值。

返回值:

類型說明
boolean是否成功對已有數(shù)據(jù)進行替換

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The replace method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("sparrow", 123);
  3. let result = hashMap.replace("sparrow", 357);

forEach

forEach(callbackFn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void

通過回調函數(shù)來遍歷HashMap實例對象上的元素以及元素對應的下標。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
callbackFnfunction回調函數(shù)。
thisArgObjectcallbackfn被調用時用作this值。

callbackfn的參數(shù)說明:

參數(shù)名類型必填說明
valueV當前遍歷到的元素鍵值對的值。
keyK當前遍歷到的元素鍵值對的鍵。
mapHashMap<K, V>當前調用forEach方法的實例對象。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The forEach method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("sparrow", 123);
  3. hashMap.set("gull", 357);
  4. hashMap.forEach((value, key) => {
  5. console.log("value:" + value, "key:" + key);
  6. });

entries

entries(): IterableIterator<[K, V]>

返回包含此映射中包含的鍵值對的新迭代器對象。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
IterableIterator<[K, V]>返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The entries method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. let iter = hashMap.entries();
  5. let temp = iter.next().value;
  6. while(temp != undefined) {
  7. console.log("key:" + temp[0]);
  8. console.log("value:" + temp[1]);
  9. temp = iter.next().value;
  10. }

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<[K, V]>

返回一個迭代器,迭代器的每一項都是一個 JavaScript 對象,并返回該對象。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
IterableIterator<[K, V]>返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID錯誤信息
10200011The Symbol.iterator method cannot be bound.

示例:

  1. let hashMap = new HashMap();
  2. hashMap.set("squirrel", 123);
  3. hashMap.set("sparrow", 356);
  4. // 使用方法一:
  5. for (let item of hashMap) {
  6. console.log("key:" + item[0]);
  7. console.log("value:" + item[1]);
  8. }
  9. // 使用方法二:
  10. let iter = hashMap[Symbol.iterator]();
  11. let temp = iter.next().value;
  12. while(temp != undefined) {
  13. console.log("key:" + temp[0]);
  14. console.log("value:" + temp[1]);
  15. temp = iter.next().value;
  16. }
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號