<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

OpenHarmony語言基礎類庫【@ohos.util.LightWeightMap (非線性容器LightWeightMap)】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-26 17:59 ? 次閱讀

LightWeightMap可用于存儲具有關聯關系的key-value鍵值對集合,存儲元素中key值唯一,每個key對應一個value。

LightWeightMap依據泛型定義,采用輕量級結構,初始默認容量大小為8,每次擴容大小為原始容量的兩倍。

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

LightWeightMap和[HashMap]都是用來存儲鍵值對的集合,LightWeightMap占用內存更小。

推薦使用場景: 當需要存取key-value鍵值對時,推薦使用占用內存更小的LightWeightMap。

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

  • K:Key,鍵
  • V:Value,值

說明:

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

導入模塊

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

LightWeightMap

屬性

系統能力: SystemCapability.Utils.Lang

名稱類型可讀可寫說明
lengthnumberLightWeightMap的元素個數。

鴻蒙開發指導文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]點擊或者復制轉到。

constructor

constructor()

LightWeightMap的構造函數。

系統能力: SystemCapability.Utils.Lang

錯誤碼:

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

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

示例:

let lightWeightMap = new LightWeightMap();

isEmpty

isEmpty(): boolean

判斷該LightWeightMap是否為空。

系統能力: SystemCapability.Utils.Lang

返回值:

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

錯誤碼:

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

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

示例:

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

hasAll

hasAll(map: LightWeightMap): boolean

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

系統能力: SystemCapability.Utils.Lang

參數

參數名類型必填說明
mapLightWeightMap比較對象。

返回值:

類型說明
boolean包含所有元素返回true,否則返回false。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

hasKey(key: K): boolean

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

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
keyK指定key。

返回值:

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

錯誤碼:

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

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

示例:

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

hasValue

hasValue(value: V): boolean

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

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
valueV指定元素。

返回值:

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

錯誤碼:

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

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

示例:

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

increaseCapacityTo

increaseCapacityTo(minimumCapacity: number): void

將當前LightWeightMap擴容至可以容納指定數量元素。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
minimumCapacitynumber需要容納的數量。

錯誤碼:

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

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

示例:

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

get

get(key: K): V

獲取指定key所對應的value。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
keyK指定key。

返回值:

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

錯誤碼:

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

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

示例:

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

getIndexOfKey

getIndexOfKey(key: K): number

查找key元素第一次出現的下標值,如果沒有找到該元素返回-1。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
keyK被查找的元素。

返回值:

類型說明
number返回key元素第一次出現時的下標值,查找失敗返回-1。

錯誤碼:

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

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

示例:

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

getIndexOfValue

getIndexOfValue(value: V): number

查找value元素第一次出現的下標值,如果沒有找到該元素返回-1。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
valueV被查找的元素。

返回值:

類型說明
number返回value元素第一次出現時的下標值,查找失敗返回-1。

錯誤碼:

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

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

示例:

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

getKeyAt

getKeyAt(index: number): K

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

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
indexnumber所查找的下標。

返回值:

類型說明
K返回該下標對應的元素鍵值對中key值。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The getKeyAt method cannot be bound.
10200001The 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

setAll(map: LightWeightMap): void

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

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
mapLightWeightMap被添加元素的lightWeightMap。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

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

向LightWeightMap中添加或更新一組數據。

系統能力: SystemCapability.Utils.Lang

參數:

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

返回值:

類型說明
Object返回添加數據后的lightWeightMap。

錯誤碼:

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

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

示例:

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

remove

remove(key: K): V

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

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
keyK指定key。

返回值:

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

錯誤碼:

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

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

示例:

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

removeAt

removeAt(index: number): boolean

刪除指定下標對應的元素。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
indexnumber指定下標。

返回值:

類型說明
boolean成功刪除元素返回true,否則返回false。

錯誤碼:

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

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

示例:

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

setValueAt

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

替換指定下標對應鍵值對中的元素。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
indexnumber指定下標。
newValueV替換鍵值對中的值。

返回值:

類型說明
boolean成功替換指定位置數據返回true,否則返回false。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The setValueAt method cannot be bound.
10200001The value of index is out of range.

示例:

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

getValueAt

getValueAt(index: number): V

獲取指定下標對應鍵值對中的元素。

系統能力: SystemCapability.Utils.Lang

參數:

參數名類型必填說明
indexnumber指定下標。

返回值:

類型說明
V返回指定下標對應鍵值對中的元素。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The getValueAt method cannot be bound.
10200001The 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

clear(): void

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

系統能力: SystemCapability.Utils.Lang

錯誤碼:

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

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

示例:

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

keys

keys(): IterableIterator

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

系統能力: SystemCapability.Utils.Lang

返回值:

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

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

values(): IterableIterator

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

系統能力: SystemCapability.Utils.Lang

返回值:

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

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

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

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

系統能力: SystemCapability.Utils.Lang

參數:

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

callbackfn的參數說明:

參數名類型必填說明
valueV當前遍歷到的元素鍵值對的值。
keyK當前遍歷到的元素鍵值對的鍵。
mapLightWeightMap當前調用forEach方法的實例對象。

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

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

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

系統能力: SystemCapability.Utils.Lang

返回值:

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

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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

toString(): String

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

系統能力: SystemCapability.Utils.Lang

返回值:

類型說明
String返回一個字符串。

錯誤碼:

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

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

示例:

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

[Symbol.iterator]

HarmonyOSOpenHarmony鴻蒙文檔籽料:mau123789是v直接拿

搜狗高速瀏覽器截圖20240326151450.png

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

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

系統能力: SystemCapability.Utils.Lang

返回值:

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

錯誤碼:

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

錯誤碼ID錯誤信息
10200011The 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;
}

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 鴻蒙
    +關注

    關注

    55

    文章

    1693

    瀏覽量

    42142
  • HarmonyOS
    +關注

    關注

    79

    文章

    1871

    瀏覽量

    29299
  • OpenHarmony
    +關注

    關注

    23

    文章

    3349

    瀏覽量

    15184
收藏 人收藏

    評論

    相關推薦

    OpenHarmony語言基礎類庫【@ohos.util.ArrayList (線性容器ArrayList)】

    ArrayList是一種線性數據結構,底層基于數組實現。ArrayList會根據實際需要動態調整容量,每次擴容增加50%。
    的頭像 發表于 04-25 18:48 ?399次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>語言</b>基礎類庫【@<b class='flag-5'>ohos.util</b>.ArrayList (<b class='flag-5'>線性</b><b class='flag-5'>容器</b>ArrayList)】

    OpenHarmony語言基礎類庫【@ohos.util.HashMap (非線性容器HashMap)】

    HashMap底層使用數組+鏈表+紅黑樹的方式實現,查詢、插入和刪除的效率都很高。HashMap存儲內容基于key-value的鍵值對映射,不能有重復的key,且一個key只能對應一個value。
    的頭像 發表于 04-25 22:12 ?570次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>語言</b>基礎類庫【@<b class='flag-5'>ohos.util</b>.HashMap (<b class='flag-5'>非線性</b><b class='flag-5'>容器</b>HashMap)】

    ArkTS語言基礎-解析

    ArkTS語言基礎是HarmonyOS系統上為應用開發者提供的常用基礎能力,主要包含能力如下圖所示。 圖1 ArkTS語言基礎
    發表于 02-20 16:44

    趕緊收藏!7大400多種組件,鴻蒙三方來了!

    方法https://gitee.com/openharmony-tpc/butterknifeassertj-ohos快速調用其他封裝https://gitee.com/openharmon
    發表于 05-07 14:07

    HarmonyOS方舟開發框架容器API的介紹與使用

    HashMap、HashSet、TreeMap、TreeSet、LightWeightMap、LightWeightSet、PlainArray七種。非線性容器中的key及value
    發表于 03-07 11:40

    OpenHarmony 3.1 Beta版本關鍵特性解析——ArkUI容器API介紹

    、HashSet、TreeMap、TreeSet、LightWeightMap、LightWeightSet、PlainArray 七種。非線性容器中的 key 及 value 的類
    發表于 04-24 14:58

    HarmonyOS/OpenHarmony應用開發-Stage模型ArkTS語言擴展能力基

    \'@ohos.app.ability.ExtensionAbility\'; 接口示例: *附件:HarmonyOSOpenHarmony應用開發-stage模型ArkTS語言擴展能力基
    發表于 04-26 10:00

    HarmonyOS非線性容器特性及使用場景

    非線性容器實現能快速查找的數據結構,其底層通過hash或者紅黑樹實現,包括HashMap、HashSet、TreeMap、TreeSet、LightWeightMap、LightWeightSet
    發表于 09-27 15:18

    HarmonyOS語言基礎開發指南上線啦!

    指南中提供了詳細的介紹和開發指導,幫助開發者全面了解并發實現、容器基礎操作、XML的生成解析與轉換等。 本期HarmonyOS開發者資料直通車帶您快速了解內容干貨~ 一、語言基礎
    發表于 10-18 16:36

    OpenHarmony C++公共基礎應用案例:HelloWorld

    1、程序簡介 該程序是基于OpenHarmony的C++公共基礎的簡單案例:HelloWorld。 該應用案例已在OpenHarmony凌蒙派-RK3568開發板(即
    發表于 11-22 11:21

    OpenHarmony C++公共基礎應用案例:Thread

    OpenHarmony C++公共基礎應用案例:Thread 1、程序簡介 該程序是基于OpenHarmony的C++公共基礎
    發表于 11-22 11:50

    HarmonyOS 非線性容器特性及使用場景

    、LightWeightMap、LightWeightSet、PlainArray 七種。非線性容器中的 key 及 value 的類型均滿足 ECMA 標準。 HashMap HashMap 可用來存儲具有
    的頭像 發表于 02-19 20:23 ?190次閱讀

    OpenHarmony語言基礎類庫【@ohos.util.Deque (線性容器Deque)】

    Deque(double ended queue)根據循環隊列的數據結構實現,符合先進先出以及先進后出的特點,支持兩端的元素插入和移除。Deque會根據實際需要動態調整容量,每次進行兩倍擴容。
    的頭像 發表于 04-25 21:17 ?73次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>語言</b>基礎類庫【@<b class='flag-5'>ohos.util</b>.Deque (<b class='flag-5'>線性</b><b class='flag-5'>容器</b>Deque)】

    OpenHarmony語言基礎類庫【@ohos.util.HashSet (非線性容器HashSet)】

    HashSet基于[HashMap]實現。在HashSet中,只對value對象進行處理。
    的頭像 發表于 04-26 15:13 ?137次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>語言</b>基礎類庫【@<b class='flag-5'>ohos.util</b>.HashSet (<b class='flag-5'>非線性</b><b class='flag-5'>容器</b>HashSet)】

    OpenHarmony語言基礎類庫【@ohos.util.TreeMap (非線性容器TreeMap)】

    TreeMap可用于存儲具有關聯關系的key-value鍵值對集合,存儲元素中key值唯一,每個key對應一個value。
    的頭像 發表于 04-28 15:23 ?97次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>語言</b>基礎類庫【@<b class='flag-5'>ohos.util</b>.TreeMap (<b class='flag-5'>非線性</b><b class='flag-5'>容器</b>TreeMap)】
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>