1# @ohos.util.LightWeightMap (Nonlinear Container LightWeightMap) 2 3**LightWeightMap** stores key-value (KV) pairs. Each key must be unique and have only one value. 4 5**LightWeightMap** is based on generics and uses a lightweight structure. Its default initial capacity is 8, and it has the capacity doubled in each expansion. 6 7The keys in such a set are searched using hash values, which are stored in an array. 8 9Compared with **[HashMap](js-apis-hashmap.md)**, which can also store KV pairs, **LightWeightMap** occupies less memory. 10 11**Recommended use case**: Use LightWeightMap when you need to store and access **KV pairs**. 12 13This topic uses the following to identify the use of generics: 14- K: Key 15- V: Value 16 17> **NOTE** 18> 19> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 20 21 22## Modules to Import 23 24```ts 25import { LightWeightMap } from '@kit.ArkTS'; 26``` 27 28## LightWeightMap 29 30### Attributes 31 32**Atomic service API**: This API can be used in atomic services since API version 12. 33 34**System capability**: SystemCapability.Utils.Lang 35 36| Name| Type| Readable| Writable| Description| 37| -------- | -------- | -------- | -------- | -------- | 38| length | number | Yes| No| Number of elements in a lightweight map (called container later).| 39 40 41### constructor 42 43constructor() 44 45A constructor used to create a **LightWeightMap** instance. 46 47**Atomic service API**: This API can be used in atomic services since API version 12. 48 49**System capability**: SystemCapability.Utils.Lang 50 51 52**Error codes** 53 54For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 55 56| ID| Error Message| 57| -------- | -------- | 58| 10200012 | The LightWeightMap's constructor cannot be directly invoked. | 59 60**Example** 61 62```ts 63let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 64``` 65 66 67### isEmpty 68 69isEmpty(): boolean 70 71Checks whether this container is empty (contains no element). 72 73**Atomic service API**: This API can be used in atomic services since API version 12. 74 75**System capability**: SystemCapability.Utils.Lang 76 77**Return value** 78 79| Type| Description| 80| -------- | -------- | 81| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 82 83**Error codes** 84 85For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 86 87| ID| Error Message| 88| -------- | -------- | 89| 10200011 | The isEmpty method cannot be bound. | 90 91**Example** 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 103Checks whether this container contains all elements of the specified **LightWeightMap** instance. 104 105**Atomic service API**: This API can be used in atomic services since API version 12. 106 107**System capability**: SystemCapability.Utils.Lang 108 109**Parameters** 110 111| Name| Type| Mandatory| Description| 112| -------- | -------- | -------- | -------- | 113| map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance to be used for comparison.| 114 115**Return value** 116 117| Type| Description| 118| -------- | -------- | 119| boolean | Returns **true** if all the elements in the specified **LightWeightMap** instance are contained; returns **false** otherwise.| 120 121**Error codes** 122 123For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 124 125| ID| Error Message| 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**Example** 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 146Checks whether this container contains the specified key. 147 148**Atomic service API**: This API can be used in atomic services since API version 12. 149 150**System capability**: SystemCapability.Utils.Lang 151 152**Parameters** 153 154| Name| Type| Mandatory| Description| 155| -------- | -------- | -------- | -------- | 156| key | K | Yes| Target key.| 157 158**Return value** 159 160| Type| Description| 161| -------- | -------- | 162| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.| 163 164**Error codes** 165 166For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 167 168| ID| Error Message| 169| -------- | -------- | 170| 10200011 | The hasKey method cannot be bound. | 171 172**Example** 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 185Checks whether this container contains the specified value. 186 187**Atomic service API**: This API can be used in atomic services since API version 12. 188 189**System capability**: SystemCapability.Utils.Lang 190 191**Parameters** 192 193| Name| Type| Mandatory| Description| 194| -------- | -------- | -------- | -------- | 195| value | V | Yes| Target value.| 196 197**Return value** 198 199| Type| Description| 200| -------- | -------- | 201| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.| 202 203**Error codes** 204 205For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 206 207| ID| Error Message| 208| -------- | -------- | 209| 10200011 | The hasValue method cannot be bound. | 210 211**Example** 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 223Increases the capacity of this container. 224 225If the passed-in capacity is greater than or equal to the number of elements in this container, the container capacity is changed to the new capacity. If the passed-in capacity is less than the number of elements in this container, the capacity is not changed. 226 227**Atomic service API**: This API can be used in atomic services since API version 12. 228 229**System capability**: SystemCapability.Utils.Lang 230 231**Parameters** 232 233| Name| Type| Mandatory| Description| 234| -------- | -------- | -------- | -------- | 235| minimumCapacity | number | Yes| Minimum number of elements to accommodate in this container.| 236 237**Error codes** 238 239For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 240 241| ID| Error Message| 242| -------- | -------- | 243| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 244| 10200011 | The increaseCapacityTo method cannot be bound. | 245 246**Example** 247 248```ts 249let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 250lightWeightMap.increaseCapacityTo(10); 251``` 252 253### get 254 255get(key: K): V 256 257Obtains the value of the specified key in this container. 258 259**Atomic service API**: This API can be used in atomic services since API version 12. 260 261**System capability**: SystemCapability.Utils.Lang 262 263**Parameters** 264 265| Name| Type| Mandatory| Description| 266| -------- | -------- | -------- | -------- | 267| key | K | Yes| Target key.| 268 269**Return value** 270 271| Type| Description| 272| -------- | -------- | 273| V | Value of the key.| 274 275**Error codes** 276 277For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 278 279| ID| Error Message| 280| -------- | -------- | 281| 10200011 | The get method cannot be bound. | 282 283**Example** 284 285```ts 286let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 287lightWeightMap.set("squirrel", 123); 288lightWeightMap.set("sparrow", 356); 289let result = lightWeightMap.get("sparrow"); 290``` 291 292 293### getIndexOfKey 294 295getIndexOfKey(key: K): number 296 297Obtains the index of the first occurrence of an element with the specified key in this container. 298 299**Atomic service API**: This API can be used in atomic services since API version 12. 300 301**System capability**: SystemCapability.Utils.Lang 302 303**Parameters** 304 305| Name| Type| Mandatory| Description| 306| -------- | -------- | -------- | -------- | 307| key | K | Yes| Key of the element.| 308 309**Return value** 310 311| Type| Description| 312| -------- | -------- | 313| number | Returns the position index if obtained; returns **-1** otherwise.| 314 315**Error codes** 316 317For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 318 319| ID| Error Message| 320| -------- | -------- | 321| 10200011 | The getIndexOfKey method cannot be bound. | 322 323**Example** 324 325```ts 326let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 327lightWeightMap.set("squirrel", 123); 328lightWeightMap.set("sparrow", 356); 329let result = lightWeightMap.getIndexOfKey("sparrow"); 330``` 331 332 333### getIndexOfValue 334 335getIndexOfValue(value: V): number 336 337Obtains the index of the first occurrence of an element with the specified value in this container. 338 339**Atomic service API**: This API can be used in atomic services since API version 12. 340 341**System capability**: SystemCapability.Utils.Lang 342 343**Parameters** 344 345| Name| Type| Mandatory| Description| 346| -------- | -------- | -------- | -------- | 347| value | V | Yes| Key of the element.| 348 349**Return value** 350 351| Type| Description| 352| -------- | -------- | 353| number | Returns the position index if obtained; returns **-1** otherwise.| 354 355**Error codes** 356 357For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 358 359| ID| Error Message| 360| -------- | -------- | 361| 10200011 | The getIndexOfValue method cannot be bound. | 362 363**Example** 364 365```ts 366let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 367lightWeightMap.set("squirrel", 123); 368lightWeightMap.set("sparrow", 356); 369let result = lightWeightMap.getIndexOfValue(123); 370``` 371 372 373### getKeyAt 374 375getKeyAt(index: number): K 376 377Obtains the key of an element at the specified position in this container. 378 379**Atomic service API**: This API can be used in atomic services since API version 12. 380 381**System capability**: SystemCapability.Utils.Lang 382 383**Parameters** 384 385| Name| Type| Mandatory| Description| 386| -------- | -------- | -------- | -------- | 387| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 388 389**Return value** 390 391| Type| Description| 392| -------- | -------- | 393| K | Returns the key if obtained; returns **undefined** otherwise.| 394 395**Error codes** 396 397For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 398 399| ID| Error Message| 400| -------- | -------- | 401| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 402| 10200001 | The value of index is out of range. | 403| 10200011 | The getKeyAt method cannot be bound. | 404 405**Example** 406 407```ts 408let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 409lightWeightMap.set("squirrel", 123); 410lightWeightMap.set("sparrow", 356); 411let result = lightWeightMap.getKeyAt(1); 412``` 413 414 415### setAll 416 417setAll(map: LightWeightMap<K, V>): void 418 419Adds all elements in a **LightWeightMap** instance to this container. 420 421**Atomic service API**: This API can be used in atomic services since API version 12. 422 423**System capability**: SystemCapability.Utils.Lang 424 425**Parameters** 426 427| Name| Type| Mandatory| Description| 428| -------- | -------- | -------- | -------- | 429| map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance whose elements are to be added to the current container.| 430 431**Error codes** 432 433For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 434 435| ID| Error Message| 436| -------- | -------- | 437| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 438| 10200011 | The setAll method cannot be bound. | 439 440**Example** 441 442```ts 443let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 444lightWeightMap.set("squirrel", 123); 445lightWeightMap.set("sparrow", 356); 446let map: LightWeightMap<string, number> = new LightWeightMap(); 447map.setAll(lightWeightMap); // Add all elements in lightWeightMap to the map. 448``` 449 450 451### set 452set(key: K, value: V): Object 453 454Adds or updates an element in this container. 455 456**Atomic service API**: This API can be used in atomic services since API version 12. 457 458**System capability**: SystemCapability.Utils.Lang 459 460**Parameters** 461 462| Name| Type| Mandatory| Description| 463| -------- | -------- | -------- | -------- | 464| key | K | Yes| Key of the target element.| 465| value | V | Yes| Value of the target element.| 466 467**Return value** 468 469| Type| Description| 470| -------- | -------- | 471| Object | Container that contains the new element.| 472 473**Error codes** 474 475For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 476 477| ID| Error Message| 478| -------- | -------- | 479| 10200011 | The set method cannot be bound. | 480 481**Example** 482 483```ts 484let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 485let result = lightWeightMap.set("squirrel", 123); 486``` 487 488 489### remove 490 491remove(key: K): V 492 493Removes an element with the specified key from this container. 494 495**Atomic service API**: This API can be used in atomic services since API version 12. 496 497**System capability**: SystemCapability.Utils.Lang 498 499**Parameters** 500 501| Name| Type| Mandatory| Description| 502| -------- | -------- | -------- | -------- | 503| key | K | Yes| Target key.| 504 505**Return value** 506 507| Type| Description| 508| -------- | -------- | 509| V | Value of the element removed.| 510 511**Error codes** 512 513For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 514 515| ID| Error Message| 516| -------- | -------- | 517| 10200011 | The remove method cannot be bound. | 518 519**Example** 520 521```ts 522let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 523lightWeightMap.set("squirrel", 123); 524lightWeightMap.set("sparrow", 356); 525lightWeightMap.remove("sparrow"); 526``` 527 528 529### removeAt 530 531removeAt(index: number): boolean 532 533Removes an element at the specified position from this container. 534 535**Atomic service API**: This API can be used in atomic services since API version 12. 536 537**System capability**: SystemCapability.Utils.Lang 538 539**Parameters** 540 541| Name| Type| Mandatory| Description| 542| -------- | -------- | -------- | -------- | 543| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 544 545**Return value** 546 547| Type| Description| 548| -------- | -------- | 549| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 550 551**Error codes** 552 553For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 554 555| ID| Error Message| 556| -------- | -------- | 557| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 558| 10200011 | The removeAt method cannot be bound. | 559 560**Example** 561 562```ts 563let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 564lightWeightMap.set("squirrel", 123); 565lightWeightMap.set("sparrow", 356); 566let result = lightWeightMap.removeAt(1); 567``` 568 569 570### setValueAt 571 572setValueAt(index: number, newValue: V): boolean 573 574Sets a value for an element at the specified position in this container. 575 576**Atomic service API**: This API can be used in atomic services since API version 12. 577 578**System capability**: SystemCapability.Utils.Lang 579 580**Parameters** 581 582| Name| Type| Mandatory| Description| 583| -------- | -------- | -------- | -------- | 584| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 585| newValue | V | Yes| Value of the target element to set.| 586 587**Return value** 588 589| Type| Description| 590| -------- | -------- | 591| boolean | Returns **true** if the value is set successfully; returns **false** otherwise.| 592 593**Error codes** 594 595For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 596 597| ID| Error Message| 598| -------- | -------- | 599| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 600| 10200001 | The value of index is out of range. | 601| 10200011 | The setValueAt method cannot be bound. | 602 603**Example** 604 605```ts 606let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 607lightWeightMap.set("squirrel", 123); 608lightWeightMap.set("sparrow", 356); 609lightWeightMap.setValueAt(1, 3546); 610``` 611 612 613### getValueAt 614 615getValueAt(index: number): V 616 617Obtains the value of an element at the specified position in this container. 618 619**Atomic service API**: This API can be used in atomic services since API version 12. 620 621**System capability**: SystemCapability.Utils.Lang 622 623**Parameters** 624 625| Name| Type| Mandatory| Description| 626| -------- | -------- | -------- | -------- | 627| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 628 629**Return value** 630 631| Type| Description| 632| -------- | -------- | 633| V | Value obtained.| 634 635**Error codes** 636 637For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 638 639| ID| Error Message| 640| -------- | -------- | 641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 642| 10200001 | The value of index is out of range. | 643| 10200011 | The getValueAt method cannot be bound. | 644 645**Example** 646 647```ts 648let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 649lightWeightMap.set("squirrel", 123); 650lightWeightMap.set("sparrow", 356); 651let result = lightWeightMap.getValueAt(1); 652``` 653 654 655### clear 656 657clear(): void 658 659Clears this container and sets its length to **0**. 660 661**Atomic service API**: This API can be used in atomic services since API version 12. 662 663**System capability**: SystemCapability.Utils.Lang 664 665**Error codes** 666 667For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 668 669| ID| Error Message| 670| -------- | -------- | 671| 10200011 | The clear method cannot be bound. | 672 673**Example** 674 675```ts 676let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 677lightWeightMap.set("squirrel", 123); 678lightWeightMap.set("sparrow", 356); 679lightWeightMap.clear(); 680``` 681 682 683### keys 684 685keys(): IterableIterator<K> 686 687Obtains an iterator that contains all the keys in this container. 688 689**Atomic service API**: This API can be used in atomic services since API version 12. 690 691**System capability**: SystemCapability.Utils.Lang 692 693**Return value** 694 695| Type| Description| 696| -------- | -------- | 697| IterableIterator<K> | Iterator obtained.| 698 699**Error codes** 700 701For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 702 703| ID| Error Message| 704| -------- | -------- | 705| 10200011 | The keys method cannot be bound. | 706 707**Example** 708 709```ts 710let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 711lightWeightMap.set("squirrel", 123); 712lightWeightMap.set("sparrow", 356); 713let iter = lightWeightMap.keys(); 714let temp: IteratorResult<string, number> = iter.next(); 715while(!temp.done) { 716 console.log("value:" + temp.value); 717 temp = iter.next(); 718} 719``` 720 721 722### values 723 724values(): IterableIterator<V> 725 726Obtains an iterator that contains all the values in this container. 727 728**Atomic service API**: This API can be used in atomic services since API version 12. 729 730**System capability**: SystemCapability.Utils.Lang 731 732**Return value** 733 734| Type| Description| 735| -------- | -------- | 736| IterableIterator<V> | Iterator obtained.| 737 738**Error codes** 739 740For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 741 742| ID| Error Message| 743| -------- | -------- | 744| 10200011 | The values method cannot be bound. | 745 746**Example** 747 748```ts 749let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 750lightWeightMap.set("squirrel", 123); 751lightWeightMap.set("sparrow", 356); 752let iter = lightWeightMap.values(); 753let temp: IteratorResult<number> = iter.next(); 754while(!temp.done) { 755 console.log("value:" + temp.value); 756 temp = iter.next(); 757} 758``` 759 760 761### forEach 762 763forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void 764 765Uses a callback to traverse the elements in this container and obtain their position indexes. 766 767**Atomic service API**: This API can be used in atomic services since API version 12. 768 769**System capability**: SystemCapability.Utils.Lang 770 771**Parameters** 772 773| Name| Type| Mandatory| Description| 774| -------- | -------- | -------- | -------- | 775| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 776| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 777 778callbackFn 779| Name| Type| Mandatory| Description| 780| -------- | -------- | -------- | -------- | 781| value | V | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.| 782| key | K | No| Key of the element that is currently traversed. The default value is the key of the first key-value pair.| 783| map | LightWeightMap<K, V> | No| Instance that calls the **forEach** API. The default value is this instance.| 784 785**Error codes** 786 787For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 788 789| ID| Error Message| 790| -------- | -------- | 791| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 792| 10200011 | The forEach method cannot be bound. | 793 794**Example** 795 796```ts 797let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 798lightWeightMap.set("sparrow", 123); 799lightWeightMap.set("gull", 357); 800lightWeightMap.forEach((value?: number, key?: string) => { 801 console.log("value:" + value, "key:" + key); 802}); 803``` 804```ts 805// You are not advised to use the set, setValueAt, remove, or removeAt APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 806let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 807for(let i = 0; i < 10; i++) { 808 lightWeightMap.set("sparrow" + i, 123); 809} 810for(let i = 0; i < 10; i++) { 811 lightWeightMap.remove("sparrow" + i); 812} 813``` 814 815### entries 816 817entries(): IterableIterator<[K, V]> 818 819Obtains an iterator that contains all the elements in this container. 820 821**Atomic service API**: This API can be used in atomic services since API version 12. 822 823**System capability**: SystemCapability.Utils.Lang 824 825**Return value** 826 827| Type| Description| 828| -------- | -------- | 829| IterableIterator<[K, V]> | Iterator obtained.| 830 831**Error codes** 832 833For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 834 835| ID| Error Message| 836| -------- | -------- | 837| 10200011 | The entries method cannot be bound. | 838 839**Example** 840 841```ts 842let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 843lightWeightMap.set("squirrel", 123); 844lightWeightMap.set("sparrow", 356); 845let iter = lightWeightMap.entries(); 846let temp: IteratorResult<Object[]> = iter.next(); 847while(!temp.done) { 848 console.log("key:" + temp.value[0]); 849 console.log("value:" + temp.value[1]); 850 temp = iter.next(); 851} 852``` 853```ts 854// You are not advised to use the set, setValueAt, remove, or removeAt APIs in entries because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 855let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 856for(let i = 0; i < 10; i++) { 857 lightWeightMap.set("sparrow" + i, 123); 858} 859for(let i = 0; i < 10; i++) { 860 lightWeightMap.remove("sparrow" + i); 861} 862``` 863 864### toString 865 866toString(): String 867 868Concatenates the elements in this container into a string and returns the string. 869 870**Atomic service API**: This API can be used in atomic services since API version 12. 871 872**System capability**: SystemCapability.Utils.Lang 873 874**Return value** 875 876 | Type| Description| 877 | -------- | -------- | 878 | String | String obtained.| 879 880**Error codes** 881 882For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 883 884| ID| Error Message| 885| -------- | -------- | 886| 10200011 | The toString method cannot be bound. | 887 888**Example** 889 890```ts 891let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 892lightWeightMap.set("squirrel", 123); 893lightWeightMap.set("sparrow", 356); 894let result = lightWeightMap.toString(); 895``` 896 897### [Symbol.iterator] 898 899[Symbol.iterator]\(): IterableIterator<[K, V]> 900 901Obtains an iterator, each item of which is a JavaScript object. 902 903**Atomic service API**: This API can be used in atomic services since API version 12. 904 905**System capability**: SystemCapability.Utils.Lang 906 907**Return value** 908 909| Type| Description| 910| -------- | -------- | 911| IterableIterator<[K, V]> | Iterator obtained.| 912 913**Error codes** 914 915For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 916 917| ID| Error Message| 918| -------- | -------- | 919| 10200011 | The Symbol.iterator method cannot be bound. | 920 921**Example** 922 923```ts 924let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 925lightWeightMap.set("squirrel", 123); 926lightWeightMap.set("sparrow", 356); 927 928// Method 1: 929let nums = Array.from(lightWeightMap.values()); 930for (let item1 of nums) { 931 console.log("value:" + item1); 932} 933 934let key = Array.from(lightWeightMap.keys()); 935for (let item2 of key) { 936 console.log("key:" + item2); 937} 938 939// Method 2: 940let iter = lightWeightMap[Symbol.iterator](); 941let temp: IteratorResult<Object[]> = iter.next(); 942while(!temp.done) { 943 console.log("key:" + temp.value[0]); 944 console.log("value:" + temp.value[1]); 945 temp = iter.next(); 946} 947``` 948```ts 949// You are not advised to use the set, setValueAt, remove, or removeAt APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 950let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 951for(let i = 0; i < 10; i++) { 952 lightWeightMap.set("sparrow" + i, 123); 953} 954for(let i = 0; i < 10; i++) { 955 lightWeightMap.remove("sparrow" + i); 956} 957``` 958