1# @ohos.util.LightWeightMap (非线性容器LightWeightMap)
2
3LightWeightMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。
4
5LightWeightMap依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。
6
7集合中key值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的key值及value值。
8
9LightWeightMap和[HashMap](js-apis-hashmap.md)都是用来存储键值对的集合,LightWeightMap占用内存更小。
10
11**推荐使用场景:** 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。
12
13文档中存在泛型的使用,涉及以下泛型标记符:<br>
14- K:Key,键<br>
15- V:Value,值
16
17> **说明:**
18>
19> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
20
21
22## 导入模块
23
24```ts
25import { LightWeightMap } from '@kit.ArkTS';
26```
27
28## LightWeightMap
29
30### 属性
31
32**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
33
34**系统能力:** SystemCapability.Utils.Lang
35
36| 名称 | 类型 | 可读 | 可写 | 说明 |
37| -------- | -------- | -------- | -------- | -------- |
38| length | number | 是 | 否 | LightWeightMap的元素个数。 |
39
40
41### constructor
42
43constructor()
44
45LightWeightMap的构造函数。
46
47**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
48
49**系统能力:** SystemCapability.Utils.Lang
50
51
52**错误码:**
53
54以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
55
56| 错误码ID | 错误信息 |
57| -------- | -------- |
58| 10200012 | The LightWeightMap's constructor cannot be directly invoked. |
59
60**示例:**
61
62```ts
63let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
64```
65
66
67### isEmpty
68
69isEmpty(): boolean
70
71判断该LightWeightMap是否为空。
72
73**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
74
75**系统能力:** SystemCapability.Utils.Lang
76
77**返回值:**
78
79| 类型 | 说明 |
80| -------- | -------- |
81| boolean | 为空返回true,不为空返回false。 |
82
83**错误码:**
84
85以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
86
87| 错误码ID | 错误信息 |
88| -------- | -------- |
89| 10200011 | The isEmpty method cannot be bound. |
90
91**示例:**
92
93```ts
94const lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
95let result = lightWeightMap.isEmpty();
96```
97
98
99### hasAll
100
101hasAll(map: LightWeightMap<K, V>): boolean
102
103判断此LightWeightMap中是否含有该指定map中的所有元素。
104
105**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
106
107**系统能力:** SystemCapability.Utils.Lang
108
109**参数:**
110
111| 参数名 | 类型 | 必填 | 说明 |
112| -------- | -------- | -------- | -------- |
113| map | LightWeightMap<K, V> | 是 | 比较对象。 |
114
115**返回值:**
116
117| 类型 | 说明 |
118| -------- | -------- |
119| boolean | 包含所有元素返回true,否则返回false。 |
120
121**错误码:**
122
123以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
124
125| 错误码ID | 错误信息 |
126| -------- | -------- |
127| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
128| 10200011 | The hasAll method cannot be bound. |
129
130**示例:**
131
132```ts
133let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
134lightWeightMap.set("squirrel", 123);
135lightWeightMap.set("sparrow", 356);
136let map: LightWeightMap<string, number> = new LightWeightMap();
137map.set("sparrow", 356);
138let result = lightWeightMap.hasAll(map);
139```
140
141
142### hasKey
143
144hasKey(key: K): boolean
145
146判断此LightWeightMap中是否含有该指定key。
147
148**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
149
150**系统能力:** SystemCapability.Utils.Lang
151
152**参数:**
153
154| 参数名 | 类型 | 必填 | 说明 |
155| -------- | -------- | -------- | -------- |
156| key | K | 是 | 指定key。 |
157
158**返回值:**
159
160| 类型 | 说明 |
161| -------- | -------- |
162| boolean | 包含指定key返回true,否则返回false。 |
163
164**错误码:**
165
166以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
167
168| 错误码ID | 错误信息 |
169| -------- | -------- |
170| 10200011 | The hasKey method cannot be bound. |
171
172**示例:**
173
174```ts
175let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
176lightWeightMap.set("squirrel", 123);
177let result = lightWeightMap.hasKey("squirrel");
178```
179
180
181### hasValue
182
183hasValue(value: V): boolean
184
185判断此LightWeightMap中是否含有该指定value。
186
187**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
188
189**系统能力:** SystemCapability.Utils.Lang
190
191**参数:**
192
193| 参数名 | 类型 | 必填 | 说明 |
194| -------- | -------- | -------- | -------- |
195| value | V | 是 | 指定元素。 |
196
197**返回值:**
198
199| 类型 | 说明 |
200| -------- | -------- |
201| boolean | 包含指定元素返回true,否则返回false。 |
202
203**错误码:**
204
205以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
206
207| 错误码ID | 错误信息 |
208| -------- | -------- |
209| 10200011 | The hasValue method cannot be bound. |
210
211**示例:**
212
213```ts
214let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
215lightWeightMap.set("squirrel", 123);
216let result = lightWeightMap.hasValue(123);
217```
218
219### increaseCapacityTo
220
221increaseCapacityTo(minimumCapacity: number): void
222
223将当前LightWeightMap扩容至可以容纳指定数量元素。如果传入的容量值大于或等于当前LightWeightMap中的元素个数,将容量变更为新容量,小于则不会变更。
224
225**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
226
227**系统能力:** SystemCapability.Utils.Lang
228
229**参数:**
230
231| 参数名 | 类型 | 必填 | 说明 |
232| -------- | -------- | -------- | -------- |
233| minimumCapacity | number | 是 | 需要容纳的元素数量。 |
234
235**错误码:**
236
237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
238
239| 错误码ID | 错误信息 |
240| -------- | -------- |
241| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
242| 10200011 | The increaseCapacityTo method cannot be bound. |
243
244**示例:**
245
246```ts
247let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
248lightWeightMap.increaseCapacityTo(10);
249```
250
251### get
252
253get(key: K): V
254
255获取指定key所对应的value。
256
257**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
258
259**系统能力:** SystemCapability.Utils.Lang
260
261**参数:**
262
263| 参数名 | 类型 | 必填 | 说明 |
264| -------- | -------- | -------- | -------- |
265| key | K | 是 | 指定key。 |
266
267**返回值:**
268
269| 类型 | 说明 |
270| -------- | -------- |
271| V | 返回key映射的value值。 |
272
273**错误码:**
274
275以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
276
277| 错误码ID | 错误信息 |
278| -------- | -------- |
279| 10200011 | The get method cannot be bound. |
280
281**示例:**
282
283```ts
284let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
285lightWeightMap.set("squirrel", 123);
286lightWeightMap.set("sparrow", 356);
287let result = lightWeightMap.get("sparrow");
288```
289
290
291### getIndexOfKey
292
293getIndexOfKey(key: K): number
294
295查找key元素第一次出现的下标值,如果没有找到该元素返回-1。
296
297**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
298
299**系统能力:** SystemCapability.Utils.Lang
300
301**参数:**
302
303| 参数名 | 类型 | 必填 | 说明 |
304| -------- | -------- | -------- | -------- |
305| key | K | 是 | 被查找的元素。 |
306
307**返回值:**
308
309| 类型 | 说明 |
310| -------- | -------- |
311| number | 返回key元素第一次出现时的下标值,查找失败返回-1。 |
312
313**错误码:**
314
315以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
316
317| 错误码ID | 错误信息 |
318| -------- | -------- |
319| 10200011 | The getIndexOfKey method cannot be bound. |
320
321**示例:**
322
323```ts
324let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
325lightWeightMap.set("squirrel", 123);
326lightWeightMap.set("sparrow", 356);
327let result = lightWeightMap.getIndexOfKey("sparrow");
328```
329
330
331### getIndexOfValue
332
333getIndexOfValue(value: V): number
334
335查找value元素第一次出现的下标值,如果没有找到该元素返回-1。
336
337**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
338
339**系统能力:** SystemCapability.Utils.Lang
340
341**参数:**
342
343| 参数名 | 类型 | 必填 | 说明 |
344| -------- | -------- | -------- | -------- |
345| value | V | 是 | 被查找的元素。 |
346
347**返回值:**
348
349| 类型 | 说明 |
350| -------- | -------- |
351| number | 返回value元素第一次出现时的下标值,查找失败返回-1。 |
352
353**错误码:**
354
355以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
356
357| 错误码ID | 错误信息 |
358| -------- | -------- |
359| 10200011 | The getIndexOfValue method cannot be bound. |
360
361**示例:**
362
363```ts
364let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
365lightWeightMap.set("squirrel", 123);
366lightWeightMap.set("sparrow", 356);
367let result = lightWeightMap.getIndexOfValue(123);
368```
369
370
371### getKeyAt
372
373getKeyAt(index: number): K
374
375查找指定下标的元素键值对中key值,否则返回undefined。
376
377**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
378
379**系统能力:** SystemCapability.Utils.Lang
380
381**参数:**
382
383| 参数名 | 类型 | 必填 | 说明 |
384| -------- | -------- | -------- | -------- |
385| index | number | 是 | 所查找的下标。需要小于等于int32_max即2147483647。 |
386
387**返回值:**
388
389| 类型 | 说明 |
390| -------- | -------- |
391| K | 返回该下标对应的元素键值对中key值。 |
392
393**错误码:**
394
395以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
396
397| 错误码ID | 错误信息 |
398| -------- | -------- |
399| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
400| 10200001 | The value of index is out of range. |
401| 10200011 | The getKeyAt method cannot be bound. |
402
403**示例:**
404
405```ts
406let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
407lightWeightMap.set("squirrel", 123);
408lightWeightMap.set("sparrow", 356);
409let result = lightWeightMap.getKeyAt(1);
410```
411
412
413### setAll
414
415setAll(map: LightWeightMap<K, V>): void
416
417将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。
418
419**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
420
421**系统能力:** SystemCapability.Utils.Lang
422
423**参数:**
424
425| 参数名 | 类型 | 必填 | 说明 |
426| -------- | -------- | -------- | -------- |
427| map | LightWeightMap<K, V> | 是 | 被添加元素的lightWeightMap。 |
428
429**错误码:**
430
431以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
432
433| 错误码ID | 错误信息 |
434| -------- | -------- |
435| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
436| 10200011 | The setAll method cannot be bound. |
437
438**示例:**
439
440```ts
441let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
442lightWeightMap.set("squirrel", 123);
443lightWeightMap.set("sparrow", 356);
444let map: LightWeightMap<string, number> = new LightWeightMap();
445map.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中
446```
447
448
449### set
450set(key: K, value: V): Object
451
452向LightWeightMap中添加或更新一组数据。
453
454**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
455
456**系统能力:** SystemCapability.Utils.Lang
457
458**参数:**
459
460| 参数名 | 类型 | 必填 | 说明 |
461| -------- | -------- | -------- | -------- |
462| key | K | 是 | 添加成员数据的键名。 |
463| value | V | 是 | 添加成员数据的值。 |
464
465**返回值:**
466
467| 类型 | 说明 |
468| -------- | -------- |
469| Object | 返回添加数据后的lightWeightMap。 |
470
471**错误码:**
472
473以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
474
475| 错误码ID | 错误信息 |
476| -------- | -------- |
477| 10200011 | The set method cannot be bound. |
478
479**示例:**
480
481```ts
482let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
483let result = lightWeightMap.set("squirrel", 123);
484```
485
486
487### remove
488
489remove(key: K): V
490
491删除并返回指定key映射的元素。
492
493**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
494
495**系统能力:** SystemCapability.Utils.Lang
496
497**参数:**
498
499| 参数名 | 类型 | 必填 | 说明 |
500| -------- | -------- | -------- | -------- |
501| key | K | 是 | 指定key。 |
502
503**返回值:**
504
505| 类型 | 说明 |
506| -------- | -------- |
507| V | 返回删除元素的值。 |
508
509**错误码:**
510
511以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
512
513| 错误码ID | 错误信息 |
514| -------- | -------- |
515| 10200011 | The remove method cannot be bound. |
516
517**示例:**
518
519```ts
520let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
521lightWeightMap.set("squirrel", 123);
522lightWeightMap.set("sparrow", 356);
523lightWeightMap.remove("sparrow");
524```
525
526
527### removeAt
528
529removeAt(index: number): boolean
530
531删除指定下标对应的元素。
532
533**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
534
535**系统能力:** SystemCapability.Utils.Lang
536
537**参数:**
538
539| 参数名 | 类型 | 必填 | 说明 |
540| -------- | -------- | -------- | -------- |
541| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 |
542
543**返回值:**
544
545| 类型 | 说明 |
546| -------- | -------- |
547| boolean | 成功删除元素返回true,否则返回false。 |
548
549**错误码:**
550
551以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
552
553| 错误码ID | 错误信息 |
554| -------- | -------- |
555| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
556| 10200011 | The removeAt method cannot be bound. |
557
558**示例:**
559
560```ts
561let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
562lightWeightMap.set("squirrel", 123);
563lightWeightMap.set("sparrow", 356);
564let result = lightWeightMap.removeAt(1);
565```
566
567
568### setValueAt
569
570setValueAt(index: number, newValue: V): boolean
571
572替换指定下标对应键值对中的元素。
573
574**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
575
576**系统能力:** SystemCapability.Utils.Lang
577
578**参数:**
579
580| 参数名 | 类型 | 必填 | 说明 |
581| -------- | -------- | -------- | -------- |
582| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 |
583| newValue | V | 是 | 替换键值对中的值。 |
584
585**返回值:**
586
587| 类型 | 说明 |
588| -------- | -------- |
589| boolean | 成功替换指定位置数据返回true,否则返回false。 |
590
591**错误码:**
592
593以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
594
595| 错误码ID | 错误信息 |
596| -------- | -------- |
597| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
598| 10200001 | The value of index is out of range. |
599| 10200011 | The setValueAt method cannot be bound. |
600
601**示例:**
602
603```ts
604let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
605lightWeightMap.set("squirrel", 123);
606lightWeightMap.set("sparrow", 356);
607lightWeightMap.setValueAt(1, 3546);
608```
609
610
611### getValueAt
612
613getValueAt(index: number): V
614
615获取指定下标对应键值对中的元素。
616
617**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
618
619**系统能力:** SystemCapability.Utils.Lang
620
621**参数:**
622
623| 参数名 | 类型 | 必填 | 说明 |
624| -------- | -------- | -------- | -------- |
625| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 |
626
627**返回值:**
628
629| 类型 | 说明 |
630| -------- | -------- |
631| V | 返回指定下标对应键值对中的元素。 |
632
633**错误码:**
634
635以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
636
637| 错误码ID | 错误信息 |
638| -------- | -------- |
639| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
640| 10200001 | The value of index is out of range. |
641| 10200011 | The getValueAt method cannot be bound. |
642
643**示例:**
644
645```ts
646let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
647lightWeightMap.set("squirrel", 123);
648lightWeightMap.set("sparrow", 356);
649let result = lightWeightMap.getValueAt(1);
650```
651
652
653### clear
654
655clear(): void
656
657清除LightWeightMap中的所有元素,并把length置为0。
658
659**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
660
661**系统能力:** SystemCapability.Utils.Lang
662
663**错误码:**
664
665以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
666
667| 错误码ID | 错误信息 |
668| -------- | -------- |
669| 10200011 | The clear method cannot be bound. |
670
671**示例:**
672
673```ts
674let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
675lightWeightMap.set("squirrel", 123);
676lightWeightMap.set("sparrow", 356);
677lightWeightMap.clear();
678```
679
680
681### keys
682
683keys(): IterableIterator&lt;K&gt;
684
685返回包含此映射中包含的键的新迭代器对象。
686
687**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
688
689**系统能力:** SystemCapability.Utils.Lang
690
691**返回值:**
692
693| 类型 | 说明 |
694| -------- | -------- |
695| IterableIterator&lt;K&gt; | 返回一个迭代器。 |
696
697**错误码:**
698
699以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
700
701| 错误码ID | 错误信息 |
702| -------- | -------- |
703| 10200011 | The keys method cannot be bound. |
704
705**示例:**
706
707```ts
708let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
709lightWeightMap.set("squirrel", 123);
710lightWeightMap.set("sparrow", 356);
711let iter = lightWeightMap.keys();
712let temp: IteratorResult<string, number> = iter.next();
713while(!temp.done) {
714  console.log("value:" + temp.value);
715  temp = iter.next();
716}
717```
718
719
720### values
721
722values(): IterableIterator&lt;V&gt;
723
724返回包含此映射中包含的键值的新迭代器对象。
725
726**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
727
728**系统能力:** SystemCapability.Utils.Lang
729
730**返回值:**
731
732| 类型 | 说明 |
733| -------- | -------- |
734| IterableIterator&lt;V&gt; | 返回一个迭代器。 |
735
736**错误码:**
737
738以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
739
740| 错误码ID | 错误信息 |
741| -------- | -------- |
742| 10200011 | The values method cannot be bound. |
743
744**示例:**
745
746```ts
747let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
748lightWeightMap.set("squirrel", 123);
749lightWeightMap.set("sparrow", 356);
750let iter = lightWeightMap.values();
751let temp: IteratorResult<number> = iter.next();
752while(!temp.done) {
753  console.log("value:" + temp.value);
754  temp = iter.next();
755}
756```
757
758
759### forEach
760
761forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
762
763通过回调函数来遍历实例对象上的元素以及元素对应的下标。
764
765**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
766
767**系统能力:** SystemCapability.Utils.Lang
768
769**参数:**
770
771| 参数名 | 类型 | 必填 | 说明 |
772| -------- | -------- | -------- | -------- |
773| callbackFn | function | 是 | 回调函数。 |
774| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
775
776callbackfn的参数说明:
777| 参数名 | 类型 | 必填 | 说明 |
778| -------- | -------- | -------- | -------- |
779| value | V | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 |
780| key | K | 否 | 当前遍历到的元素键值对的键,默认值为首个键值对的键。 |
781| map | LightWeightMap<K, V> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
782
783**错误码:**
784
785以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
786
787| 错误码ID | 错误信息 |
788| -------- | -------- |
789| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
790| 10200011 | The forEach method cannot be bound. |
791
792**示例:**
793
794```ts
795let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
796lightWeightMap.set("sparrow", 123);
797lightWeightMap.set("gull", 357);
798lightWeightMap.forEach((value?: number, key?: string) => {
799  console.log("value:" + value, "key:" + key);
800});
801```
802```ts
803// 不建议在forEach中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
804let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
805for(let i = 0; i < 10; i++) {
806  lightWeightMap.set("sparrow" + i, 123);
807}
808for(let i = 0; i < 10; i++) {
809  lightWeightMap.remove("sparrow" + i);
810}
811```
812
813### entries
814
815entries(): IterableIterator<[K, V]>
816
817返回包含此映射中包含的键值对的新迭代器对象。
818
819**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
820
821**系统能力:** SystemCapability.Utils.Lang
822
823**返回值:**
824
825| 类型 | 说明 |
826| -------- | -------- |
827| IterableIterator<[K, V]> | 返回一个迭代器。 |
828
829**错误码:**
830
831以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
832
833| 错误码ID | 错误信息 |
834| -------- | -------- |
835| 10200011 | The entries method cannot be bound. |
836
837**示例:**
838
839```ts
840let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
841lightWeightMap.set("squirrel", 123);
842lightWeightMap.set("sparrow", 356);
843let iter = lightWeightMap.entries();
844let temp: IteratorResult<Object[]> = iter.next();
845while(!temp.done) {
846  console.log("key:" + temp.value[0]);
847  console.log("value:" + temp.value[1]);
848  temp = iter.next();
849}
850```
851```ts
852// 不建议在entries中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
853let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
854for(let i = 0; i < 10; i++) {
855  lightWeightMap.set("sparrow" + i, 123);
856}
857for(let i = 0; i < 10; i++) {
858  lightWeightMap.remove("sparrow" + i);
859}
860```
861
862### toString
863
864toString(): String
865
866将此映射中包含的键值对拼接成字符串,并返回字符串类型。
867
868**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
869
870**系统能力:** SystemCapability.Utils.Lang
871
872**返回值:**
873
874  | 类型 | 说明 |
875  | -------- | -------- |
876  | String | 返回一个字符串。 |
877
878**错误码:**
879
880以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
881
882| 错误码ID | 错误信息 |
883| -------- | -------- |
884| 10200011 | The toString method cannot be bound. |
885
886**示例:**
887
888```ts
889let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
890lightWeightMap.set("squirrel", 123);
891lightWeightMap.set("sparrow", 356);
892let result = lightWeightMap.toString();
893```
894
895### [Symbol.iterator]
896
897[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
898
899返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
900
901**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
902
903**系统能力:** SystemCapability.Utils.Lang
904
905**返回值:**
906
907| 类型 | 说明 |
908| -------- | -------- |
909| IterableIterator<[K, V]> | 返回一个迭代器。 |
910
911**错误码:**
912
913以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
914
915| 错误码ID | 错误信息 |
916| -------- | -------- |
917| 10200011 | The Symbol.iterator method cannot be bound. |
918
919**示例:**
920
921```ts
922let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
923lightWeightMap.set("squirrel", 123);
924lightWeightMap.set("sparrow", 356);
925
926// 使用方法一:
927let nums = Array.from(lightWeightMap.values());
928for (let item1 of nums) {
929  console.log("value:" + item1);
930}
931
932let key = Array.from(lightWeightMap.keys());
933for (let item2 of key) {
934  console.log("key:" + item2);
935}
936
937// 使用方法二:
938let iter = lightWeightMap[Symbol.iterator]();
939let temp: IteratorResult<Object[]> = iter.next();
940while(!temp.done) {
941  console.log("key:" + temp.value[0]);
942  console.log("value:" + temp.value[1]);
943  temp = iter.next();
944}
945```
946```ts
947// 不建议在Symbol.iterator中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
948let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
949for(let i = 0; i < 10; i++) {
950  lightWeightMap.set("sparrow" + i, 123);
951}
952for(let i = 0; i < 10; i++) {
953  lightWeightMap.remove("sparrow" + i);
954}
955```