非線性容器LightWeightMap

2024-01-23 18:13 更新
說明

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

LightWeightMap可用于存儲具有關(guān)聯(lián)關(guān)系的key-value鍵值對集合,存儲元素中key值唯一,每個key對應(yīng)一個value。

LightWeightMap依據(jù)泛型定義,采用輕量級結(jié)構(gòu),初始默認(rèn)容量大小為8,每次擴(kuò)容大小為原始容量的兩倍。

集合中key值的查找依賴于hash算法,通過一個數(shù)組存儲hash值,然后映射到其他數(shù)組中的key值及value值。

LightWeightMap和HashMap都是用來存儲鍵值對的集合,LightWeightMap占用內(nèi)存更小。

推薦使用場景: 當(dāng)需要存取key-value鍵值對時,推薦使用占用內(nèi)存更小的LightWeightMap。

文檔中存在泛型的使用,涉及以下泛型標(biāo)記符:

  • K: Key, 鍵

  • V: Value, 值

導(dǎo)入模塊

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

LightWeightMap

屬性

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

名稱

類型

可讀

可寫

說明

length

number

LightWeightMap的元素個數(shù)。

constructor

constructor()

LightWeightMap的構(gòu)造函數(shù)。

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

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200012

The LightWeightMap's constructor cannot be directly invoked.

示例:

  1. let lightWeightMap = new LightWeightMap();

isEmpty

isEmpty(): boolean

判斷該LightWeightMap是否為空。

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

返回值:

類型

說明

boolean

為空返回true,不為空返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The isEmpty method cannot be bound.

示例:

  1. const lightWeightMap = new LightWeightMap();
  2. let result = lightWeightMap.isEmpty();

hasAll

hasAll(map: LightWeightMap<K, V>): boolean

判斷此LightWeightMap中是否含有該指定map中的所有元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

map

LightWeightMap<K, V>

比較對象。

返回值:

類型

說明

boolean

包含所有元素返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The hasAll method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let map = new LightWeightMap();
  5. map.set("sparrow", 356);
  6. let result = lightWeightMap.hasAll(map);

hasKey

hasKey(key: K): boolean

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

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

參數(shù):

參數(shù)名

類型

必填

說明

key

K

指定key。

返回值:

類型

說明

boolean

包含指定key返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The hasKey method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. let result = lightWeightMap.hasKey("squirrel");

hasValue

hasValue(value: V): boolean

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

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

參數(shù):

參數(shù)名

類型

必填

說明

value

V

指定元素。

返回值:

類型

說明

boolean

包含指定元素返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The hasValue method cannot be bound.

示例:

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

increaseCapacityTo

increaseCapacityTo(minimumCapacity: number): void

將當(dāng)前LightWeightMap擴(kuò)容至可以容納指定數(shù)量元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

minimumCapacity

number

需要容納的數(shù)量。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The increaseCapacityTo method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.increaseCapacityTo(10);

get

get(key: K): V

獲取指定key所對應(yīng)的value。

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

參數(shù):

參數(shù)名

類型

必填

說明

key

K

指定key。

返回值:

類型

說明

V

返回key映射的value值。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The get method cannot be bound.

示例:

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

getIndexOfKey

getIndexOfKey(key: K): number

查找key元素第一次出現(xiàn)的下標(biāo)值,如果沒有找到該元素返回-1。

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

參數(shù):

參數(shù)名

類型

必填

說明

key

K

被查找的元素。

返回值:

類型

說明

number

返回key元素第一次出現(xiàn)時的下標(biāo)值,查找失敗返回-1。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The getIndexOfKey method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getIndexOfKey("sparrow");

getIndexOfValue

getIndexOfValue(value: V): number

查找value元素第一次出現(xiàn)的下標(biāo)值,如果沒有找到該元素返回-1。

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

參數(shù):

參數(shù)名

類型

必填

說明

value

V

被查找的元素。

返回值:

類型

說明

number

返回value元素第一次出現(xiàn)時的下標(biāo)值,查找失敗返回-1。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The getIndexOfValue method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getIndexOfValue(123);

getKeyAt

getKeyAt(index: number): K

查找指定下標(biāo)的元素鍵值對中key值,否則返回undefined。

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

參數(shù):

參數(shù)名

類型

必填

說明

index

number

所查找的下標(biāo)。

返回值:

類型

說明

K

返回該下標(biāo)對應(yīng)的元素鍵值對中key值。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The getKeyAt method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getKeyAt(1);

setAll

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

將一個LightWeightMap中的所有元素組添加到另一個lightWeightMap中。

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

參數(shù):

參數(shù)名

類型

必填

說明

map

LightWeightMap<K, V>

被添加元素的lightWeightMap。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The setAll method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let map = new LightWeightMap();
  5. map.setAll(lightWeightMap); // 將lightWeightMap中所有的元素添加到map中

set

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

向LightWeightMap中添加或更新一組數(shù)據(jù)。

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

參數(shù):

參數(shù)名

類型

必填

說明

key

K

添加成員數(shù)據(jù)的鍵名。

value

V

添加成員數(shù)據(jù)的值。

返回值:

類型

說明

Object

返回添加數(shù)據(jù)后的lightWeightMap。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The set method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. let result = lightWeightMap.set("squirrel", 123);

remove

remove(key: K): V

刪除并返回指定key映射的元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

key

K

指定key。

返回值:

類型

說明

V

返回刪除元素的值。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The remove method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. lightWeightMap.remove("sparrow");

removeAt

removeAt(index: number): boolean

刪除指定下標(biāo)對應(yīng)的元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定下標(biāo)。

返回值:

類型

說明

boolean

成功刪除元素返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The removeAt method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.removeAt(1);

setValueAt

setValueAt(index: number, newValue: V): boolean

替換指定下標(biāo)對應(yīng)鍵值對中的元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定下標(biāo)。

newValue

V

替換鍵值對中的值。

返回值:

類型

說明

boolean

成功替換指定位置數(shù)據(jù)返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The setValueAt method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. lightWeightMap.setValueAt(1, 3546);

getValueAt

getValueAt(index: number): V

獲取指定下標(biāo)對應(yīng)鍵值對中的元素。

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

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定下標(biāo)。

返回值:

類型

說明

V

返回指定下標(biāo)對應(yīng)鍵值對中的元素。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The getValueAt method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getValueAt(1);

clear

clear(): void

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

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

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The clear method cannot be bound.

示例:

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

keys

keys(): IterableIterator<K>

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

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

返回值:

類型

說明

IterableIterator<K>

返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The keys method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let iter = lightWeightMap.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>

返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The values method cannot be bound.

示例:

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

forEach

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

通過回調(diào)函數(shù)來遍歷實(shí)例對象上的元素以及元素對應(yīng)的下標(biāo)。

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

參數(shù):

參數(shù)名

類型

必填

說明

callbackFn

function

回調(diào)函數(shù)。

thisArg

Object

callbackfn被調(diào)用時用作this值。

callbackfn的參數(shù)說明:

參數(shù)名

類型

必填

說明

value

V

當(dāng)前遍歷到的元素鍵值對的值。

key

K

當(dāng)前遍歷到的元素鍵值對的鍵。

map

LightWeightMap<K, V>

當(dāng)前調(diào)用forEach方法的實(shí)例對象。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The forEach method cannot be bound.

示例:

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

entries

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

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

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

返回值:

類型

說明

IterableIterator<[K, V]>

返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The entries method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let iter = lightWeightMap.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. }

toString

toString(): String

將此映射中包含的鍵值對拼接成字符串,并返回字符串類型。

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

返回值:

類型

說明

String

返回一個字符串。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The toString method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.toString();

[Symbol.iterator]

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

返回一個迭代器,迭代器的每一項(xiàng)都是一個 JavaScript 對象,并返回該對象。

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

返回值:

類型

說明

IterableIterator<[K, V]>

返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The Symbol.iterator method cannot be bound.

示例:

  1. let lightWeightMap = new LightWeightMap();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. // 使用方法一:
  5. for (let item of lightWeightMap) {
  6. console.log("key:" + item[0]);
  7. console.log("value:" + item[1]);
  8. }
  9. // 使用方法二:
  10. let iter = lightWeightMap[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

公眾號
微信公眾號

編程獅公眾號