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