W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
本模塊首批接口從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, 鍵
constructor()
LightWeightMap的構(gòu)造函數(shù)。
系統(tǒng)能力: SystemCapability.Utils.Lang
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200012 | The LightWeightMap's constructor cannot be directly invoked. |
示例:
- let lightWeightMap = new LightWeightMap();
isEmpty(): boolean
判斷該LightWeightMap是否為空。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
boolean | 為空返回true,不為空返回false。 |
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The isEmpty method cannot be bound. |
示例:
- const lightWeightMap = new LightWeightMap();
- let result = lightWeightMap.isEmpty();
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let map = new LightWeightMap();
- map.set("sparrow", 356);
- let result = lightWeightMap.hasAll(map);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- let result = lightWeightMap.hasKey("squirrel");
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- let result = lightWeightMap.hasValue(123);
- lightWeightMap.set("squirrel", 123);
- let result1 = lightWeightMap.hasValue(123);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.increaseCapacityTo(10);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.get("sparrow");
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.getIndexOfKey("sparrow");
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.getIndexOfValue(123);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.getKeyAt(1);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let map = new LightWeightMap();
- map.setAll(lightWeightMap); // 將lightWeightMap中所有的元素添加到map中
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- let result = lightWeightMap.set("squirrel", 123);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- lightWeightMap.remove("sparrow");
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.removeAt(1);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- lightWeightMap.setValueAt(1, 3546);
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.getValueAt(1);
clear(): void
清除LightWeightMap中的所有元素,并把length置為0。
系統(tǒng)能力: SystemCapability.Utils.Lang
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The clear method cannot be bound. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- lightWeightMap.clear();
keys(): IterableIterator<K>
返回包含此映射中包含的鍵的新迭代器對象。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<K> | 返回一個迭代器。 |
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The keys method cannot be bound. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let iter = lightWeightMap.keys();
- let temp = iter.next().value;
- while(temp != undefined) {
- console.log("value:" + temp);
- temp = iter.next().value;
- }
values(): IterableIterator<V>
返回包含此映射中包含的鍵值的新迭代器對象。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<V> | 返回一個迭代器。 |
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The values method cannot be bound. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let iter = lightWeightMap.values();
- let temp = iter.next().value;
- while(temp != undefined) {
- console.log("value:" + temp);
- temp = iter.next().value;
- }
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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("sparrow", 123);
- lightWeightMap.set("gull", 357);
- lightWeightMap.forEach((value, key) => {
- console.log("value:" + value, "key:" + key);
- });
entries(): IterableIterator<[K, V]>
返回包含此映射中包含的鍵值對的新迭代器對象。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
IterableIterator<[K, V]> | 返回一個迭代器。 |
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The entries method cannot be bound. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let iter = lightWeightMap.entries();
- let temp = iter.next().value;
- while(temp != undefined) {
- console.log("key:" + temp[0]);
- console.log("value:" + temp[1]);
- temp = iter.next().value;
- }
toString(): String
將此映射中包含的鍵值對拼接成字符串,并返回字符串類型。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
String | 返回一個字符串。 |
錯誤碼:
以下錯誤碼的詳細(xì)介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200011 | The toString method cannot be bound. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- let result = lightWeightMap.toString();
[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. |
示例:
- let lightWeightMap = new LightWeightMap();
- lightWeightMap.set("squirrel", 123);
- lightWeightMap.set("sparrow", 356);
- // 使用方法一:
- for (let item of lightWeightMap) {
- console.log("key:" + item[0]);
- console.log("value:" + item[1]);
- }
- // 使用方法二:
- let iter = lightWeightMap[Symbol.iterator]();
- let temp = iter.next().value;
- while(temp != undefined) {
- console.log("key:" + temp[0]);
- console.log("value:" + temp[1]);
- temp = iter.next().value;
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: