文档管理中心

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

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

容器类使用静态语言实现,限制了存储位置和属性,不支持自定义属性和方法。

规格限制

当LightWeightMap存入的key为number类型且值大于INT32_MAX(即2147483647)或小于INT32_MIN(即-2147483648)时,针对LightWeightMap的操作,其结果可能与预期不一致。

这是因为,当key为number类型且值大于INT32_MAX或小于INT32_MIN时,存储结构会发生改变。

例如在以下示例针对key的计算中,1758783600000大于INT32_MAX,此时会通过TaggedDouble存储;1758783600小于INT32_MIN,此时会通过TaggedInt存储。由于以上存储方式的差异,当对其进行hash算法即会计算出不同的hash值,从而导致映射结果不同,产生与预期不一致的现象。

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<number, number>();
  2. let key = 1758783600000 / 1000; // 1758783600000 > INT32_MAX
  3. lightWeightMap.set(key, 1001);
  4. console.info('result:', lightWeightMap.hasKey(1758783600)); // result: false
  5. console.info('result:', lightWeightMap.hasKey(key)); // result: true

导入模块

收起
自动换行
深色代码主题
复制
  1. import { LightWeightMap } from '@kit.ArkTS';

LightWeightMap

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

属性

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

系统能力: SystemCapability.Utils.Lang

展开
名称 类型 只读 可选 说明
length number LightWeightMap的元素个数。

constructor

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

constructor()

LightWeightMap的构造函数,创建一个空的LightWeightMap实例。

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

系统能力: SystemCapability.Utils.Lang

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();

isEmpty

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

isEmpty(): boolean

判断LightWeightMap是否为空。

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. let result = lightWeightMap.isEmpty();
  3. console.info("result:", result); // result: true

hasAll

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
map LightWeightMap<K, V> 用于比较的LightWeightMap对象,判断当前实例是否包含此map中的所有元素。

返回值:

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

错误码:

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

展开
错误码ID 错误信息
10200011 The hasAll method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let targetMap = new LightWeightMap<string, number>();
  5. targetMap.set("sparrow", 356);
  6. let result = lightWeightMap.hasAll(targetMap);
  7. console.info("result = ", result); // result = true

hasKey

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

hasKey(key: K): boolean

判断LightWeightMap中是否包含指定key。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致,详见规格限制

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
key K 指定key。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致。

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. let result = lightWeightMap.hasKey("squirrel");
  4. console.info("result:", result); // result: true

hasValue

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

hasValue(value: V): boolean

判断LightWeightMap中是否包含指定value。

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
value V 要判断是否包含的value。

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. let result = lightWeightMap.hasValue(123);
  4. console.info("result:", result); // result: true

increaseCapacityTo

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

increaseCapacityTo(minimumCapacity: number): void

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

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
minimumCapacity number 需要容纳的元素数量。取值需大于等于0,大于等于当前元素个数时扩容生效,否则不变更容量。

错误码:

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

展开
错误码ID 错误信息
10200011 The increaseCapacityTo method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.increaseCapacityTo(10);

get

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

get(key: K): V

获取指定key所对应的value。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.get("sparrow");
  5. console.info("result:", result); // result: 356

getIndexOfKey

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

getIndexOfKey(key: K): number

查找key元素首次出现的下标值,如果未找到返回-1。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getIndexOfKey("sparrow");
  5. console.info("result:", result); // result: 0

getIndexOfValue

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getIndexOfValue(123);
  5. console.info("result:", result); // result: 1

getKeyAt

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

getKeyAt(index: number): K

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

展开
类型 说明
K 返回该下标对应的元素键值对中key值,如果未找到则返回undefined。

错误码:

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

展开
错误码ID 错误信息
10200001 The value of index is out of range.
10200011 The getKeyAt method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getKeyAt(1);
  5. console.info("result:", result); // result: squirrel

setAll

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

将一个LightWeightMap中的所有元素添加到另一个LightWeightMap中,如果目标LightWeightMap中已存在相同的key,则会更新其对应的value。

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
map LightWeightMap<K, V> 提供添加元素的LightWeightMap。

错误码:

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

展开
错误码ID 错误信息
10200011 The setAll method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let map = new LightWeightMap<string, number>();
  5. map.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中
  6. let result = map.get("sparrow");
  7. console.info("result:", result); // result: 356

set

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

向LightWeightMap中添加或更新一组数据。调用成功后,若key不存在则新增键值对且length增加,若key已存在则更新对应value值。

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
key K 添加或更新成员数据的键名。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致。
value V 添加或更新成员数据的值。

返回值:

展开
类型 说明
Object 返回添加或更新后的LightWeightMap实例对象。

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. let result = lightWeightMap.set("squirrel", 123);
  3. console.info("result:", result); // result: squirrel:123

remove

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

remove(key: K): V

删除指定key映射的元素。当key为number类型且值大于INT32_MAX或小于INT32_MIN时,结果可能与预期不一致。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("sparrow", 356);
  3. let result = lightWeightMap.remove("sparrow");
  4. console.info("result:", result); // result: 356

removeAt

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

removeAt(index: number): boolean

删除指定下标对应的元素。调用成功后,若下标有效则该位置的键值对从LightWeightMap中移除且length减少。

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
index number 要删除的元素的下标位置。取值范围:[0, length-1],需小于等于INT32_MAX即2147483647。

返回值:

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

错误码:

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

展开
错误码ID 错误信息
10200011 The removeAt method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.removeAt(1);
  5. console.info("result:", result); // result: true

setValueAt

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

替换指定下标对应键值对中的值。调用成功后,指定下标处键值对的值将被替换为newValue。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

展开
类型 说明
boolean 成功替换返回true,否则返回false。

错误码:

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

展开
错误码ID 错误信息
10200001 The value of index is out of range.
10200011 The setValueAt method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. lightWeightMap.setValueAt(1, 3546);
  5. console.info("result:", lightWeightMap.get("squirrel")); // result: 3546

getValueAt

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

getValueAt(index: number): V

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

展开
错误码ID 错误信息
10200001 The value of index is out of range.
10200011 The getValueAt method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.getValueAt(1);
  5. console.info("result:", result); // result: 123

clear

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

clear(): void

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

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

系统能力: SystemCapability.Utils.Lang

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. lightWeightMap.clear();
  5. let result = lightWeightMap.isEmpty();
  6. console.info("result:", result); // result: true

keys

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

keys(): IterableIterator<K>

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

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. // 不建议在keys中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
  2. let lightWeightMap = new LightWeightMap<string, number>();
  3. lightWeightMap.set("squirrel", 123);
  4. lightWeightMap.set("sparrow", 356);
  5. let keys = lightWeightMap.keys();
  6. for (let key of keys) {
  7. console.info("key:", key);
  8. }
  9. // key: sparrow
  10. // key: squirrel

values

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

values(): IterableIterator<V>

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

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. // 不建议在values中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
  2. let lightWeightMap = new LightWeightMap<string, number>();
  3. lightWeightMap.set("squirrel", 123);
  4. lightWeightMap.set("sparrow", 356);
  5. let values = lightWeightMap.values();
  6. for (let value of values) {
  7. console.info("value:", value);
  8. }
  9. // value: 356
  10. // value: 123

forEach

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

通过回调函数来遍历实例对象上的元素及其键值对信息。

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

系统能力: SystemCapability.Utils.Lang

参数:

展开
参数名 类型 必填 说明
callbackFn function 回调函数,用于遍历LightWeightMap实例中的元素及下标。
thisArg Object callbackFn被调用时用作this值。当需要回调函数中的this指向非当前实例对象时传入此参数,当不需要改变this指向时可不传入。不传入时,默认值为当前实例对象。

callbackFn的参数说明:

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

错误码:

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

展开
错误码ID 错误信息
10200011 The forEach method cannot be bound.

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("sparrow", 123);
  3. lightWeightMap.set("gull", 357);
  4. lightWeightMap.forEach((value: number, key: string) => {
  5. console.info("value:" + value, "key:" + key);
  6. });
  7. // value:123 key:sparrow
  8. // value:357 key:gull
收起
自动换行
深色代码主题
复制
  1. // 不建议在forEach中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
  2. let lightWeightMap = new LightWeightMap<string, number>();
  3. for (let i = 0; i < 10; i++) {
  4. lightWeightMap.set("sparrow" + i, 123);
  5. }
  6. for (let i = 0; i < 10; i++) {
  7. lightWeightMap.remove("sparrow" + i);
  8. }

entries

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

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

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

系统能力: SystemCapability.Utils.Lang

返回值:

展开
类型 说明
IterableIterator<[K, V]> 返回包含此映射中所有键值对的迭代器对象。

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let iteratorResult = lightWeightMap.entries();
  5. let temp: IteratorResult<Object[]> = iteratorResult.next();
  6. while (!temp.done) {
  7. console.info("key:" + temp.value[0]);
  8. console.info("value:" + temp.value[1]);
  9. temp = iteratorResult.next();
  10. }
收起
自动换行
深色代码主题
复制
  1. // 不建议在entries中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
  2. let lightWeightMap = new LightWeightMap<string, number>();
  3. for(let i = 0; i < 10; i++) {
  4. lightWeightMap.set("sparrow" + i, 123);
  5. }
  6. for(let i = 0; i < 10; i++) {
  7. lightWeightMap.remove("sparrow" + i);
  8. }

toString

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

toString(): String

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

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

系统能力: SystemCapability.Utils.Lang

返回值:

展开
类型 说明
String 返回将此映射中键值对拼接而成的字符串。

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. let result = lightWeightMap.toString();
  5. console.info("result:", result); // result: sparrow:356,squirrel:123

[Symbol.iterator]

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

返回一个迭代器,迭代器的每一项都是一个包含键和值的[K, V]数组。

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

收起
自动换行
深色代码主题
复制
  1. let lightWeightMap = new LightWeightMap<string, number>();
  2. lightWeightMap.set("squirrel", 123);
  3. lightWeightMap.set("sparrow", 356);
  4. // 使用方法一:
  5. for (let item of lightWeightMap) {
  6. console.info("key:", item[0]);
  7. console.info("value:", item[1]);
  8. }
  9. // key: sparrow
  10. // value: 356
  11. // key: squirrel
  12. // value: 123
  13. // 使用方法二:
  14. let iter = lightWeightMap[Symbol.iterator]();
  15. let temp: IteratorResult<Object[]> = iter.next();
  16. while(!temp.done) {
  17. console.info("key:", temp.value[0]);
  18. console.info("value:", temp.value[1]);
  19. temp = iter.next();
  20. }
  21. // key: sparrow
  22. // value: 356
  23. // key: squirrel
  24. // value: 123
收起
自动换行
深色代码主题
复制
  1. // 不建议在Symbol.iterator中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
  2. let lightWeightMap = new LightWeightMap<string, number>();
  3. for(let i = 0; i < 10; i++) {
  4. lightWeightMap.set("sparrow" + i, 123);
  5. }
  6. for(let i = 0; i < 10; i++) {
  7. lightWeightMap.remove("sparrow" + i);
  8. }
在 API参考 中进行搜索
请输入您想要搜索的关键词