@ohos.util.LightWeightMap (非线性容器LightWeightMap)

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 '@kit.ArkTS';

LightWeightMap

属性

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

名称 类型 可读 可写 说明
length number LightWeightMap的元素个数。

constructor

constructor()

LightWeightMap的构造函数。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200012 The LightWeightMap's constructor cannot be directly invoked.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();

isEmpty

isEmpty(): boolean

判断该LightWeightMap是否为空。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
boolean 为空返回true,不为空返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The isEmpty method cannot be bound.

示例:

const lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
let result = lightWeightMap.isEmpty();

hasAll

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

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

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
map LightWeightMap<K, V> 比较对象。

返回值:

类型 说明
boolean 包含所有元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011 The hasAll method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let map: LightWeightMap<string, number> = new LightWeightMap<string, number>();
map.set("sparrow", 356);
let result = lightWeightMap.hasAll(map);

hasKey

hasKey(key: K): boolean

判断此LightWeightMap中是否含有该指定key。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
key K 指定key。

返回值:

类型 说明
boolean 包含指定key返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The hasKey method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
let result = lightWeightMap.hasKey("squirrel");

hasValue

hasValue(value: V): boolean

判断此LightWeightMap中是否含有该指定value。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
value V 指定元素。

返回值:

类型 说明
boolean 包含指定元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The hasValue method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
let result = lightWeightMap.hasValue(123);

increaseCapacityTo

increaseCapacityTo(minimumCapacity: number): void

将当前LightWeightMap扩容至可以容纳指定数量元素。如果传入的容量值大于或等于当前LightWeightMap中的元素个数,将容量变更为新容量,小于则不会变更。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
minimumCapacity number 需要容纳的元素数量。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011 The increaseCapacityTo method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.increaseCapacityTo(10);

get

get(key: K): V

获取指定key所对应的value。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

参数:

参数名 类型 必填 说明
key K 指定key。

返回值:

类型 说明
V 返回key映射的value值。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The get method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.get("sparrow");

get20+

get(key: K): V | undefined

获取指定key所对应的value值。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
key K 指定key。

返回值:

类型 说明
V | undefined 返回key映射的value值,不存在返回undefined。

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.get("sparrow");

getIndexOfKey

getIndexOfKey(key: K): number

查找key元素第一次出现的下标值,如果没有找到该元素返回-1。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
key K 被查找的元素。

返回值:

类型 说明
number 返回key元素第一次出现时的下标值,查找失败返回-1。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The getIndexOfKey method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfKey("sparrow");

getIndexOfValue

getIndexOfValue(value: V): number

查找value元素第一次出现的下标值,如果没有找到该元素返回-1。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
value V 被查找的元素。

返回值:

类型 说明
number 返回value元素第一次出现时的下标值,查找失败返回-1。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The getIndexOfValue method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfValue(123);

getKeyAt

getKeyAt(index: number): K

查找指定下标的元素键值对中key值,否则返回undefined。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

参数:

参数名 类型 必填 说明
index number 所查找的下标。需要小于等于int32_max即2147483647。

返回值:

类型 说明
K 返回该下标对应的元素键值对中key值。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200001 The value of index is out of range.
10200011 The getKeyAt method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getKeyAt(1);

getKeyAt20+

getKeyAt(index: number): K | undefined

查找指定下标的元素键值对中key值,否则返回undefined。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
index number 所查找的下标。需要小于等于int32_max即2147483647。

返回值:

类型 说明
K 返回该下标对应的元素键值对中key值。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
10200001 The value of index is out of range.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getKeyAt(1);

setAll

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

将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
map LightWeightMap<K, V> 被添加元素的lightWeightMap。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011 The setAll method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let map: LightWeightMap<string, number> = new LightWeightMap();
map.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中

set

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

向LightWeightMap中添加或更新一组数据。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
key K 添加成员数据的键名。
value V 添加成员数据的值。

返回值:

类型 说明
Object 返回添加数据后的lightWeightMap。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The set method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
let result = lightWeightMap.set("squirrel", 123);

remove

remove(key: K): V

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

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

参数:

参数名 类型 必填 说明
key K 指定key。

返回值:

类型 说明
V 返回删除元素的值。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The remove method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.remove("sparrow");

remove20+

remove(key: K): V | undefined

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

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
key K 指定key。

返回值:

类型 说明
V | undefined 返回删除元素的值,key不存在则返回undefined。

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.remove("sparrow");

removeAt

removeAt(index: number): boolean

删除指定下标对应的元素。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
index number 指定下标。需要小于等于int32_max即2147483647。

返回值:

类型 说明
boolean 成功删除元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011 The removeAt method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.removeAt(1);

setValueAt

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

替换指定下标对应键值对中的元素。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
index number 指定下标。需要小于等于int32_max即2147483647。
newValue V 替换键值对中的值。

返回值:

类型 说明
boolean 成功替换指定位置数据返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200001 The value of index is out of range.
10200011 The setValueAt method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.setValueAt(1, 3546);

getValueAt

getValueAt(index: number): V

获取指定下标对应键值对中的元素。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

参数:

参数名 类型 必填 说明
index number 指定下标。需要小于等于int32_max即2147483647。

返回值:

类型 说明
V 返回指定下标对应键值对中的元素。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200001 The value of index is out of range.
10200011 The getValueAt method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getValueAt(1);

getValueAt20+

getValueAt(index: number): V | undefined

获取指定下标对应键值对中的元素。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
index number 指定下标。需要小于等于int32_max即2147483647。

返回值:

类型 说明
V | undefined 返回指定下标对应键值对中的元素,如果没有则返回undefined。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
10200001 The value of index is out of range.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getValueAt(1);

clear

clear(): void

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

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The clear method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.clear();

keys

keys(): IterableIterator<K>

返回包含此映射中包含的键的新迭代器对象。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<K> 返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The keys method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.keys();
let temp: IteratorResult<string> = iter.next();
while(!temp.done) {
  console.info("value:" + temp.value);
  temp = iter.next();
}

values

values(): IterableIterator<V>

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

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<V> 返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The values method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.values();
let temp: IteratorResult<number> = iter.next();
while(!temp.done) {
  console.info("value:" + temp.value);
  temp = iter.next();
}

forEach

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

通过回调函数来遍历实例对象上的元素以及元素对应的下标。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

参数:

参数名 类型 必填 说明
callbackFn function 回调函数。
thisArg Object callbackfn被调用时用作this值,默认值为当前实例对象。

callbackfn的参数说明:

参数名 类型 必填 说明
value V 当前遍历到的元素键值对的值,默认值为首个键值对的值。
key K 当前遍历到的元素键值对的键,默认值为首个键值对的键。
map LightWeightMap<K, V> 当前调用forEach方法的实例对象,默认值为当前实例对象。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011 The forEach method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("gull", 357);
lightWeightMap.forEach((value?: number, key?: string) => {
  console.log("value:" + value, "key:" + key);
});
// 不建议在forEach中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
for(let i = 0; i < 10; i++) {
  lightWeightMap.set("sparrow" + i, 123);
}
for(let i = 0; i < 10; i++) {
  lightWeightMap.remove("sparrow" + i);
}

forEach20+

forEach(callbackFn: LightWeightMapCbFn<K, V>)

通过回调函数来遍历实例对象上的元素以及元素对应的下标。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
callbackFn LightWeightMapCbFn<K, V> 回调函数。

示例:

import { LightWeightMapCbFn } from '@ohos.util.LightWeightMap';

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap<string, number>();
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("test", 987);
lightWeightMap.set("gull", 357);
let lightWeightMapCb: LightWeightMapCbFn<string, number> = (value: number, key: string, map: LightWeightMap<string, number>) => {
  console.info("value: " + value, " key: " + key);
};
lightWeightMap.forEach(lightWeightMapCb);

entries

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

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

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<[K, V]> 返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The entries method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, Number> = new LightWeightMap<string, Number>();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.entries();
let temp: IteratorResult<[string, Number]> = iter.next();
while(!temp.done) {
  console.log("key:" + temp.value![0]);
  console.log("value:" + temp.value![1]);
  temp = iter.next();
}
// 不建议在entries中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let lightWeightMap: LightWeightMap<string, Number> = new LightWeightMap<string, Number>();
for(let i = 0; i < 10; i++) {
  lightWeightMap.set("sparrow" + i, 123);
}
for(let i = 0; i < 10; i++) {
  lightWeightMap.remove("sparrow" + i);
}

toString

toString(): String

将此映射中包含的键值对拼接成字符串,并返回字符串类型。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
String 返回一个字符串。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The toString method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.toString();

[Symbol.iterator]

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

返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.1。

返回值:

类型 说明
IterableIterator<[K, V]> 返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The Symbol.iterator method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);

// 使用方法一:
let nums = Array.from(lightWeightMap.values());
for (let item1 of nums) {
  console.log("value:" + item1);
}

let key = Array.from(lightWeightMap.keys());
for (let item2 of key) {
  console.log("key:" + item2);
}

// 使用方法二:
let iter = lightWeightMap[Symbol.iterator]();
let temp: IteratorResult<Object[]> = iter.next();
while(!temp.done) {
  console.log("key:" + temp.value[0]);
  console.log("value:" + temp.value[1]);
  temp = iter.next();
}
// 不建议在Symbol.iterator中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
for(let i = 0; i < 10; i++) {
  lightWeightMap.set("sparrow" + i, 123);
}
for(let i = 0; i < 10; i++) {
  lightWeightMap.remove("sparrow" + i);
}

$_iterator20+

$_iterator(): IterableIterator<[K, V]>

返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

返回值:

类型 说明
IterableIterator<[K, V]> 返回一个迭代器。

示例:

let lightWeightMap: LightWeightMap<string, Number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);

// 使用方法一:
for (let item of lightWeightMap) {
  console.info("key:" + item[0]);
  console.info("value:" + item[1]);
}

// 使用方法二:
let iter = lightWeightMap.$_iterator();
let temp: IteratorResult<[string, Number]> = iter.next();
while(!temp.done) {
  console.info("key:" + temp.value![0]);
  console.info("value:" + temp.value![1]);
  temp = iter.next();
}

LightWeightMapCbFn<K, V>20+

type LightWeightMapCbFn<K, V> = (value: V, key: K, map: LightWeightMap<K, V>) => void

LightWeightMap中forEach方法的回调函数。

原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Utils.Lang

ArkTS版本: 该接口仅适用于ArkTS1.2。

参数:

参数名 类型 必填 说明
value V 当前遍历到的元素键值对的值。
key K 当前遍历到的元素键值对的键。
map LightWeightMap<K, V> 当前调用forEach方法的实例对象。