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