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