js-apis-lightweightmap.md 15.0 KB
Newer Older
L
linhaoran 已提交
1 2 3 4 5
# 非线性容器LightWeightMap  

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

6 7 8 9 10 11 12
LightWeightMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。

LightWeightMap依据泛型定义,采用轻量级结构,集合中key值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的key值及value值。

LightWeightMap和[HashMap](js-apis-hashmap.md)都是用来存储键值对的集合,LightWeightMap占用内存更小。

**推荐使用场景:** 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。
L
linhaoran 已提交
13

L
lengchangjing 已提交
14 15 16 17
文档中存在泛型的使用,涉及以下泛型标记符:<br>
- K: Key, 键<br>
- V: Value, 值

L
linhaoran 已提交
18 19
## 导入模块

20
```ts
21
import LightWeightMap from '@ohos.util.LightWeightMap';  
L
linhaoran 已提交
22 23 24 25 26 27 28 29
```



## LightWeightMap

### 属性

Z
zengyawen 已提交
30 31
**系统能力:** SystemCapability.Utils.Lang

L
linhaoran 已提交
32 33
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
34
| length | number | 是 | 否 | LightWeightMap的元素个数。 |
L
linhaoran 已提交
35 36 37 38


### constructor

Z
zengyawen 已提交
39
constructor()
L
linhaoran 已提交
40 41 42

LightWeightMap的构造函数。

Z
zengyawen 已提交
43 44
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
45 46
**示例:**

47
```ts
Z
zengyawen 已提交
48 49
let lightWeightMap = new LightWeightMap();
```
L
linhaoran 已提交
50 51 52 53


### isEmpty

Z
zengyawen 已提交
54
isEmpty(): boolean
L
linhaoran 已提交
55 56 57

判断该LightWeightMap是否为空。

Z
zengyawen 已提交
58 59
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
60
**返回值:**
L
linhaoran 已提交
61

Z
zengyawen 已提交
62 63 64 65 66 67
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |

**示例:**

68
```ts
Z
zengyawen 已提交
69
const lightWeightMap = new LightWeightMap();
70
let result = lightWeightMap.isEmpty();
Z
zengyawen 已提交
71
```
L
linhaoran 已提交
72 73 74 75


### hasAll

Z
zengyawen 已提交
76
hasAll(map: LightWeightMap<K, V>): boolean
L
linhaoran 已提交
77 78 79

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

Z
zengyawen 已提交
80 81
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
82 83 84 85 86 87 88
**参数:**

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

**返回值:**
L
linhaoran 已提交
89

Z
zengyawen 已提交
90 91 92
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含所有元素返回true,否则返回false。 |
L
linhaoran 已提交
93

Z
zengyawen 已提交
94 95
**示例:**

96
```ts
Z
zengyawen 已提交
97
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
98 99
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
100
let map = new LightWeightMap();
L
lengchangjing 已提交
101
map.set("sparrow", 356);
Z
zengyawen 已提交
102 103
let result = lightWeightMap.hasAll(map);
```
L
linhaoran 已提交
104 105 106 107 108 109 110 111


### hasKey

hasKey(key: K): boolean;

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

Z
zengyawen 已提交
112 113
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
114
**参数:**
L
linhaoran 已提交
115

Z
zengyawen 已提交
116 117
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
118
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
119

Z
zengyawen 已提交
120 121 122 123
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
124
| boolean | 包含指定key返回true,否则返回false。 |
Z
zengyawen 已提交
125 126 127

**示例:**

128
```ts
Z
zengyawen 已提交
129
let lightWeightMap = new LightWeightMap();
130
let result = lightWeightMap.hasKey;
L
lengchangjing 已提交
131 132 133
lightWeightMap.hasKey("squirrel");
lightWeightMap.set("squirrel", 123);
let result1 = lightWeightMap.hasKey("squirrel");
Z
zengyawen 已提交
134
```
L
linhaoran 已提交
135 136 137 138


### hasValue

Z
zengyawen 已提交
139
hasValue(value: V): boolean
L
linhaoran 已提交
140 141 142

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

Z
zengyawen 已提交
143 144
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
145 146 147 148 149
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | V | 是 | 指定元素。 |
L
linhaoran 已提交
150

Z
zengyawen 已提交
151
**返回值:**
L
linhaoran 已提交
152

Z
zengyawen 已提交
153 154 155 156 157 158
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |

**示例:**

159
```ts
Z
zengyawen 已提交
160
let lightWeightMap = new LightWeightMap();
161
let result = lightWeightMap.hasValue(123);
L
lengchangjing 已提交
162
lightWeightMap.set("squirrel", 123);
163
let result1 = lightWeightMap.hasValue(123);
Z
zengyawen 已提交
164
```
L
linhaoran 已提交
165 166 167 168


### increaseCapacityTo

Z
zengyawen 已提交
169
increaseCapacityTo(minimumCapacity: number): void
L
linhaoran 已提交
170 171 172

将当前LightWeightMap扩容至可以容纳指定数量元素。

Z
zengyawen 已提交
173 174
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
175
**参数:**
L
linhaoran 已提交
176

Z
zengyawen 已提交
177 178 179
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | 是 | 需要容纳的数量。 |
L
linhaoran 已提交
180

Z
zengyawen 已提交
181 182
**示例:**

183
```ts
Z
zengyawen 已提交
184 185 186
let lightWeightMap = new LightWeightMap();
lightWeightMap.increaseCapacityTo(10);
```
L
linhaoran 已提交
187 188 189 190


### get

Z
zengyawen 已提交
191
get(key: K): V
L
linhaoran 已提交
192 193 194

获取指定key所对应的value。

Z
zengyawen 已提交
195 196
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
197
**参数:**
L
linhaoran 已提交
198

Z
zengyawen 已提交
199 200 201
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
202

Z
zengyawen 已提交
203 204 205 206 207 208 209 210
**返回值:**

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

**示例:**

211
```ts
Z
zengyawen 已提交
212
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
213 214 215
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.get("sparrow");
Z
zengyawen 已提交
216
```
L
linhaoran 已提交
217 218 219 220


### getIndexOfKey

Z
zengyawen 已提交
221 222 223
getIndexOfKey(key: K): number

查找指定元素第一次出现的下标值,如果没有找到该元素返回-1。
L
linhaoran 已提交
224

Z
zengyawen 已提交
225 226
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
227
**参数:**
L
linhaoran 已提交
228

Z
zengyawen 已提交
229 230 231
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 被查找的元素。 |
L
linhaoran 已提交
232

Z
zengyawen 已提交
233
**返回值:**
L
linhaoran 已提交
234

Z
zengyawen 已提交
235 236 237 238 239 240
| 类型 | 说明 |
| -------- | -------- |
| number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |

**示例:**

241
```ts
Z
zengyawen 已提交
242
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
243 244 245
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfKey("sparrow");
Z
zengyawen 已提交
246
```
L
linhaoran 已提交
247 248 249 250


### getIndexOfValue

Z
zengyawen 已提交
251 252 253 254
getIndexOfValue(value: V): number

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

Z
zengyawen 已提交
255 256
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
257 258 259 260 261
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | V | 是 | 被查找的元素。 |
L
linhaoran 已提交
262

Z
zengyawen 已提交
263
**返回值:**
L
linhaoran 已提交
264

Z
zengyawen 已提交
265 266 267
| 类型 | 说明 |
| -------- | -------- |
| number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |
L
linhaoran 已提交
268

Z
zengyawen 已提交
269
**示例:**
L
linhaoran 已提交
270

271
```ts
Z
zengyawen 已提交
272
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
273 274
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
275
let result = lightWeightMap.getIndexOfValue(123);
Z
zengyawen 已提交
276
```
L
linhaoran 已提交
277 278 279 280


### getKeyAt

Z
zengyawen 已提交
281
getKeyAt(index: number): K
L
linhaoran 已提交
282 283 284

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

Z
zengyawen 已提交
285 286
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
287 288 289 290 291 292 293
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 所查找的下标。 |

**返回值:**
L
linhaoran 已提交
294

Z
zengyawen 已提交
295 296 297
| 类型 | 说明 |
| -------- | -------- |
| K | 返回该下标对应的元素键值对中key值。 |
L
linhaoran 已提交
298

Z
zengyawen 已提交
299 300
**示例:**

301
```ts
Z
zengyawen 已提交
302
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
303 304
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
305
let result = lightWeightMap.getKeyAt(1);
Z
zengyawen 已提交
306
```
L
linhaoran 已提交
307 308 309 310


### setAll

Z
zengyawen 已提交
311
setAll(map: LightWeightMap<K, V>): void
L
linhaoran 已提交
312

313
将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。
L
linhaoran 已提交
314

Z
zengyawen 已提交
315 316
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
317
**参数:**
L
linhaoran 已提交
318

Z
zengyawen 已提交
319 320
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
321
| map | LightWeightMap<K, V> | 是 | 被添加元素的lightWeightMap。 |
Z
zengyawen 已提交
322 323 324

**示例:**

325
```ts
Z
zengyawen 已提交
326
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
327 328
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
329 330 331
let map = new LightWeightMap();
lightWeightMap.setAll(map);
```
L
linhaoran 已提交
332 333 334


### set
Z
zengyawen 已提交
335
set(key: K, value: V): Object
L
linhaoran 已提交
336 337 338

向LightWeightMap中添加一组数据。

Z
zengyawen 已提交
339 340
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
341 342 343 344 345 346
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 添加成员数据的键名。 |
| value | V | 是 | 添加成员数据的值。 |
L
linhaoran 已提交
347

Z
zengyawen 已提交
348
**返回值:**
L
linhaoran 已提交
349

Z
zengyawen 已提交
350 351
| 类型 | 说明 |
| -------- | -------- |
352
| Object | 返回添加数据后的lightWeightMap。 |
Z
zengyawen 已提交
353 354 355

**示例:**

356
```ts
Z
zengyawen 已提交
357
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
358
let result = lightWeightMap.set("squirrel", 123);
Z
zengyawen 已提交
359
```
L
linhaoran 已提交
360 361 362 363


### remove

Z
zengyawen 已提交
364
remove(key: K): V
L
linhaoran 已提交
365

366
删除并返回指定key映射的元素。
L
linhaoran 已提交
367

Z
zengyawen 已提交
368 369
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
370 371 372 373
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
374
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
375

Z
zengyawen 已提交
376
**返回值:**
L
linhaoran 已提交
377

Z
zengyawen 已提交
378 379 380 381 382 383
| 类型 | 说明 |
| -------- | -------- |
| V | 返回删除元素的值。 |

**示例:**

384
```ts
Z
zengyawen 已提交
385
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
386 387 388
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.remove("sparrow");
Z
zengyawen 已提交
389
```
L
linhaoran 已提交
390 391 392 393


### removeAt

Z
zengyawen 已提交
394
removeAt(index: number): boolean
L
linhaoran 已提交
395

396
删除指定下标对应的元素。
L
linhaoran 已提交
397

Z
zengyawen 已提交
398 399
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
400 401 402 403
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
404
| index | number | 是 | 指定下标。 |
Z
zengyawen 已提交
405 406

**返回值:**
L
linhaoran 已提交
407

Z
zengyawen 已提交
408 409 410
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功删除元素返回true,否则返回false。 |
L
linhaoran 已提交
411

Z
zengyawen 已提交
412 413
**示例:**

414
```ts
Z
zengyawen 已提交
415
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
416 417
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
418
let result = lightWeightMap.removeAt(1);
Z
zengyawen 已提交
419
```
L
linhaoran 已提交
420 421 422 423


### setValueAt

Z
zengyawen 已提交
424
setValueAt(index: number, newValue: V): boolean
L
linhaoran 已提交
425

426
替换指定下标对应键值对中的元素。
L
linhaoran 已提交
427

Z
zengyawen 已提交
428 429
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
430
**参数:**
L
linhaoran 已提交
431

Z
zengyawen 已提交
432 433
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
434
| index | number | 是 | 指定下标。 |
Z
zengyawen 已提交
435
| newValue | V | 是 | 替换键值对中的值。 |
L
linhaoran 已提交
436

Z
zengyawen 已提交
437 438 439 440 441 442 443 444
**返回值:**

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

**示例:**

445
```ts
Z
zengyawen 已提交
446
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
447 448
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
449 450
lightWeightMap.setValueAt(1, 3546);
```
L
linhaoran 已提交
451 452 453 454


### getValueAt

Z
zengyawen 已提交
455
getValueAt(index: number): V
L
linhaoran 已提交
456

457
获取指定下标对应键值对中的元素。
L
linhaoran 已提交
458

Z
zengyawen 已提交
459 460
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
461 462 463 464
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
465
| index | number | 是 | 指定下标。 |
L
linhaoran 已提交
466

Z
zengyawen 已提交
467
**返回值:**
L
linhaoran 已提交
468

Z
zengyawen 已提交
469 470
| 类型 | 说明 |
| -------- | -------- |
471
| V | 返回指定下标对应键值对中的元素。 |
Z
zengyawen 已提交
472 473 474

**示例:**

475
```ts
Z
zengyawen 已提交
476
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
477 478
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
479
let result = lightWeightMap.getValueAt(1);
Z
zengyawen 已提交
480
```
L
linhaoran 已提交
481 482 483 484


### clear

Z
zengyawen 已提交
485
clear(): void
L
linhaoran 已提交
486 487 488

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

Z
zengyawen 已提交
489 490
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
491 492
**示例:**

493
```ts
Z
zengyawen 已提交
494
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
495 496
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
497 498
lightWeightMap.clear();
```
L
linhaoran 已提交
499 500 501 502


### keys

Z
zengyawen 已提交
503
keys(): IterableIterator&lt;K&gt;
L
linhaoran 已提交
504 505 506

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

Z
zengyawen 已提交
507 508
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
509 510 511 512 513 514 515 516
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;K&gt; | 返回一个迭代器。 |

**示例:**

517
```ts
Z
zengyawen 已提交
518
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
519 520
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
521 522 523
let iter = lightWeightMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
524
  console.log("value:" + temp);
Z
zengyawen 已提交
525 526 527
  temp = iter.next().value;
}
```
L
linhaoran 已提交
528 529 530 531


### values

Z
zengyawen 已提交
532
values(): IterableIterator&lt;V&gt;
L
linhaoran 已提交
533

W
wusongqing 已提交
534
返回包含此映射中包含的键值的新迭代器对象。
L
linhaoran 已提交
535

Z
zengyawen 已提交
536 537
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
538 539 540 541 542 543 544 545
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;V&gt; | 返回一个迭代器。 |

**示例:**

546
```ts
Z
zengyawen 已提交
547
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
548 549
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
550 551 552
let iter = lightWeightMap.values();
let temp = iter.next().value;
while(temp != undefined) {
553
  console.log("value:" + temp);
Z
zengyawen 已提交
554 555 556
  temp = iter.next().value;
} 
```
L
linhaoran 已提交
557 558 559 560


### forEach

561
forEach(callbackfn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
L
linhaoran 已提交
562 563 564

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

Z
zengyawen 已提交
565 566
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
567 568 569 570 571 572 573 574 575 576
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackfn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |

callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
577 578
| value | V | 否 | 当前遍历到的元素键值对的值。 |
| key | K | 否 | 当前遍历到的元素键值对的键。 |
579
| map | LightWeightMap<K, V> | 否 | 当前调用forEach方法的实例对象。 |
Z
zengyawen 已提交
580 581 582

**示例:**

583
```ts
Z
zengyawen 已提交
584
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
585 586
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("gull", 357);
Z
zengyawen 已提交
587
lightWeightMap.forEach((value, key) => {
588
  console.log("value:" + value, key);
Z
zengyawen 已提交
589 590
});
```
L
linhaoran 已提交
591 592 593 594


### entries

Z
zengyawen 已提交
595
entries(): IterableIterator<[K, V]>
L
linhaoran 已提交
596

597
返回包含此映射中包含的键值对的新迭代器对象。
L
linhaoran 已提交
598

Z
zengyawen 已提交
599 600
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
601 602 603 604 605 606 607 608
**返回值:**

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

**示例:**

609
```ts
Z
zengyawen 已提交
610
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
611 612
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
613 614 615
let iter = lightWeightMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
616 617
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
618 619 620
  temp = iter.next().value;
}
```
L
linhaoran 已提交
621

622 623
### toString

624
toString(): String
625 626 627

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

Z
zengyawen 已提交
628 629
**系统能力:** SystemCapability.Utils.Lang

630 631 632 633
**返回值:**

  | 类型 | 说明 |
  | -------- | -------- |
634
  | String | 返回一个字符串。 |
635 636 637

**示例:**

638
  ```ts
639
  let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
640 641
  lightWeightMap.set("squirrel", 123);
  lightWeightMap.set("sparrow", 356);
642 643
  let iter = lightWeightMap.toString();
  ```
L
linhaoran 已提交
644 645 646

### [Symbol.iterator]

Z
zengyawen 已提交
647
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
L
linhaoran 已提交
648 649 650

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

Z
zengyawen 已提交
651 652
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
653 654 655 656 657 658 659 660
**返回值:**

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

**示例:**

661
```ts
Z
zengyawen 已提交
662
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
663 664
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
665 666 667

// 使用方法一:
for (let item of lightWeightMap) { 
668 669
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
Z
zengyawen 已提交
670 671 672 673 674 675
}

// 使用方法二:
let iter = lightWeightMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
676 677
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
678 679 680
  temp = iter.next().value;
}
```