1e41f4b71Sopenharmony_ci# @ohos.util.LightWeightMap (非线性容器LightWeightMap) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciLightWeightMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciLightWeightMap依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci集合中key值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的key值及value值。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciLightWeightMap和[HashMap](js-apis-hashmap.md)都是用来存储键值对的集合,LightWeightMap占用内存更小。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci**推荐使用场景:** 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci文档中存在泛型的使用,涉及以下泛型标记符:<br> 14e41f4b71Sopenharmony_ci- K:Key,键<br> 15e41f4b71Sopenharmony_ci- V:Value,值 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci> **说明:** 18e41f4b71Sopenharmony_ci> 19e41f4b71Sopenharmony_ci> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## 导入模块 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci```ts 25e41f4b71Sopenharmony_ciimport { LightWeightMap } from '@kit.ArkTS'; 26e41f4b71Sopenharmony_ci``` 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## LightWeightMap 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci### 属性 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 37e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 38e41f4b71Sopenharmony_ci| length | number | 是 | 否 | LightWeightMap的元素个数。 | 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci### constructor 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciconstructor() 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciLightWeightMap的构造函数。 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**错误码:** 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 57e41f4b71Sopenharmony_ci| -------- | -------- | 58e41f4b71Sopenharmony_ci| 10200012 | The LightWeightMap's constructor cannot be directly invoked. | 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**示例:** 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_ci判断该LightWeightMap是否为空。 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**返回值:** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci| 类型 | 说明 | 80e41f4b71Sopenharmony_ci| -------- | -------- | 81e41f4b71Sopenharmony_ci| boolean | 为空返回true,不为空返回false。 | 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**错误码:** 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 88e41f4b71Sopenharmony_ci| -------- | -------- | 89e41f4b71Sopenharmony_ci| 10200011 | The isEmpty method cannot be bound. | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**示例:** 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_ci判断此LightWeightMap中是否含有该指定map中的所有元素。 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**参数:** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 112e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 113e41f4b71Sopenharmony_ci| map | LightWeightMap<K, V> | 是 | 比较对象。 | 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**返回值:** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| 类型 | 说明 | 118e41f4b71Sopenharmony_ci| -------- | -------- | 119e41f4b71Sopenharmony_ci| boolean | 包含所有元素返回true,否则返回false。 | 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci**错误码:** 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 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**示例:** 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_ci判断此LightWeightMap中是否含有该指定key。 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**参数:** 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 155e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 156e41f4b71Sopenharmony_ci| key | K | 是 | 指定key。 | 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**返回值:** 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci| 类型 | 说明 | 161e41f4b71Sopenharmony_ci| -------- | -------- | 162e41f4b71Sopenharmony_ci| boolean | 包含指定key返回true,否则返回false。 | 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci**错误码:** 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 169e41f4b71Sopenharmony_ci| -------- | -------- | 170e41f4b71Sopenharmony_ci| 10200011 | The hasKey method cannot be bound. | 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**示例:** 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_ci判断此LightWeightMap中是否含有该指定value。 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**参数:** 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 194e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 195e41f4b71Sopenharmony_ci| value | V | 是 | 指定元素。 | 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci**返回值:** 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci| 类型 | 说明 | 200e41f4b71Sopenharmony_ci| -------- | -------- | 201e41f4b71Sopenharmony_ci| boolean | 包含指定元素返回true,否则返回false。 | 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**错误码:** 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 208e41f4b71Sopenharmony_ci| -------- | -------- | 209e41f4b71Sopenharmony_ci| 10200011 | The hasValue method cannot be bound. | 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**示例:** 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_ci将当前LightWeightMap扩容至可以容纳指定数量元素。如果传入的容量值大于或等于当前LightWeightMap中的元素个数,将容量变更为新容量,小于则不会变更。 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**参数:** 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 232e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 233e41f4b71Sopenharmony_ci| minimumCapacity | number | 是 | 需要容纳的元素数量。 | 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci**错误码:** 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 240e41f4b71Sopenharmony_ci| -------- | -------- | 241e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 242e41f4b71Sopenharmony_ci| 10200011 | The increaseCapacityTo method cannot be bound. | 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**示例:** 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci```ts 247e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 248e41f4b71Sopenharmony_cilightWeightMap.increaseCapacityTo(10); 249e41f4b71Sopenharmony_ci``` 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci### get 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ciget(key: K): V 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci获取指定key所对应的value。 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**参数:** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 264e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 265e41f4b71Sopenharmony_ci| key | K | 是 | 指定key。 | 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci**返回值:** 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci| 类型 | 说明 | 270e41f4b71Sopenharmony_ci| -------- | -------- | 271e41f4b71Sopenharmony_ci| V | 返回key映射的value值。 | 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**错误码:** 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 278e41f4b71Sopenharmony_ci| -------- | -------- | 279e41f4b71Sopenharmony_ci| 10200011 | The get method cannot be bound. | 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**示例:** 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci```ts 284e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 285e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 286e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 287e41f4b71Sopenharmony_cilet result = lightWeightMap.get("sparrow"); 288e41f4b71Sopenharmony_ci``` 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci### getIndexOfKey 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_cigetIndexOfKey(key: K): number 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci查找key元素第一次出现的下标值,如果没有找到该元素返回-1。 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci**参数:** 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 304e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 305e41f4b71Sopenharmony_ci| key | K | 是 | 被查找的元素。 | 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci**返回值:** 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci| 类型 | 说明 | 310e41f4b71Sopenharmony_ci| -------- | -------- | 311e41f4b71Sopenharmony_ci| number | 返回key元素第一次出现时的下标值,查找失败返回-1。 | 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci**错误码:** 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 318e41f4b71Sopenharmony_ci| -------- | -------- | 319e41f4b71Sopenharmony_ci| 10200011 | The getIndexOfKey method cannot be bound. | 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci**示例:** 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci```ts 324e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 325e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 326e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 327e41f4b71Sopenharmony_cilet result = lightWeightMap.getIndexOfKey("sparrow"); 328e41f4b71Sopenharmony_ci``` 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci### getIndexOfValue 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_cigetIndexOfValue(value: V): number 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci查找value元素第一次出现的下标值,如果没有找到该元素返回-1。 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci**参数:** 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 344e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 345e41f4b71Sopenharmony_ci| value | V | 是 | 被查找的元素。 | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**返回值:** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci| 类型 | 说明 | 350e41f4b71Sopenharmony_ci| -------- | -------- | 351e41f4b71Sopenharmony_ci| number | 返回value元素第一次出现时的下标值,查找失败返回-1。 | 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**错误码:** 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 358e41f4b71Sopenharmony_ci| -------- | -------- | 359e41f4b71Sopenharmony_ci| 10200011 | The getIndexOfValue method cannot be bound. | 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci**示例:** 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci```ts 364e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 365e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 366e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 367e41f4b71Sopenharmony_cilet result = lightWeightMap.getIndexOfValue(123); 368e41f4b71Sopenharmony_ci``` 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci### getKeyAt 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_cigetKeyAt(index: number): K 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci查找指定下标的元素键值对中key值,否则返回undefined。 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**参数:** 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 384e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 385e41f4b71Sopenharmony_ci| index | number | 是 | 所查找的下标。需要小于等于int32_max即2147483647。 | 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci**返回值:** 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci| 类型 | 说明 | 390e41f4b71Sopenharmony_ci| -------- | -------- | 391e41f4b71Sopenharmony_ci| K | 返回该下标对应的元素键值对中key值。 | 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci**错误码:** 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 398e41f4b71Sopenharmony_ci| -------- | -------- | 399e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 400e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 401e41f4b71Sopenharmony_ci| 10200011 | The getKeyAt method cannot be bound. | 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci**示例:** 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci```ts 406e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 407e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 408e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 409e41f4b71Sopenharmony_cilet result = lightWeightMap.getKeyAt(1); 410e41f4b71Sopenharmony_ci``` 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci### setAll 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_cisetAll(map: LightWeightMap<K, V>): void 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ci将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci**参数:** 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 426e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 427e41f4b71Sopenharmony_ci| map | LightWeightMap<K, V> | 是 | 被添加元素的lightWeightMap。 | 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci**错误码:** 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 434e41f4b71Sopenharmony_ci| -------- | -------- | 435e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 436e41f4b71Sopenharmony_ci| 10200011 | The setAll method cannot be bound. | 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**示例:** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci```ts 441e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 442e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 443e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 444e41f4b71Sopenharmony_cilet map: LightWeightMap<string, number> = new LightWeightMap(); 445e41f4b71Sopenharmony_cimap.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中 446e41f4b71Sopenharmony_ci``` 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci### set 450e41f4b71Sopenharmony_ciset(key: K, value: V): Object 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci向LightWeightMap中添加或更新一组数据。 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci**参数:** 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 461e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 462e41f4b71Sopenharmony_ci| key | K | 是 | 添加成员数据的键名。 | 463e41f4b71Sopenharmony_ci| value | V | 是 | 添加成员数据的值。 | 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**返回值:** 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci| 类型 | 说明 | 468e41f4b71Sopenharmony_ci| -------- | -------- | 469e41f4b71Sopenharmony_ci| Object | 返回添加数据后的lightWeightMap。 | 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**错误码:** 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 476e41f4b71Sopenharmony_ci| -------- | -------- | 477e41f4b71Sopenharmony_ci| 10200011 | The set method cannot be bound. | 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci**示例:** 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci```ts 482e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 483e41f4b71Sopenharmony_cilet result = lightWeightMap.set("squirrel", 123); 484e41f4b71Sopenharmony_ci``` 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci### remove 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ciremove(key: K): V 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci删除并返回指定key映射的元素。 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci**参数:** 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 500e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 501e41f4b71Sopenharmony_ci| key | K | 是 | 指定key。 | 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci**返回值:** 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci| 类型 | 说明 | 506e41f4b71Sopenharmony_ci| -------- | -------- | 507e41f4b71Sopenharmony_ci| V | 返回删除元素的值。 | 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci**错误码:** 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 514e41f4b71Sopenharmony_ci| -------- | -------- | 515e41f4b71Sopenharmony_ci| 10200011 | The remove method cannot be bound. | 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci**示例:** 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci```ts 520e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 521e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 522e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 523e41f4b71Sopenharmony_cilightWeightMap.remove("sparrow"); 524e41f4b71Sopenharmony_ci``` 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci### removeAt 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ciremoveAt(index: number): boolean 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci删除指定下标对应的元素。 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci**参数:** 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 540e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 541e41f4b71Sopenharmony_ci| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 | 542e41f4b71Sopenharmony_ci 543e41f4b71Sopenharmony_ci**返回值:** 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci| 类型 | 说明 | 546e41f4b71Sopenharmony_ci| -------- | -------- | 547e41f4b71Sopenharmony_ci| boolean | 成功删除元素返回true,否则返回false。 | 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci**错误码:** 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 554e41f4b71Sopenharmony_ci| -------- | -------- | 555e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 556e41f4b71Sopenharmony_ci| 10200011 | The removeAt method cannot be bound. | 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci**示例:** 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci```ts 561e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 562e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 563e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 564e41f4b71Sopenharmony_cilet result = lightWeightMap.removeAt(1); 565e41f4b71Sopenharmony_ci``` 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci### setValueAt 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_cisetValueAt(index: number, newValue: V): boolean 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci替换指定下标对应键值对中的元素。 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 575e41f4b71Sopenharmony_ci 576e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci**参数:** 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 581e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 582e41f4b71Sopenharmony_ci| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 | 583e41f4b71Sopenharmony_ci| newValue | V | 是 | 替换键值对中的值。 | 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci**返回值:** 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci| 类型 | 说明 | 588e41f4b71Sopenharmony_ci| -------- | -------- | 589e41f4b71Sopenharmony_ci| boolean | 成功替换指定位置数据返回true,否则返回false。 | 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci**错误码:** 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 596e41f4b71Sopenharmony_ci| -------- | -------- | 597e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 598e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 599e41f4b71Sopenharmony_ci| 10200011 | The setValueAt method cannot be bound. | 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci**示例:** 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci```ts 604e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 605e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 606e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 607e41f4b71Sopenharmony_cilightWeightMap.setValueAt(1, 3546); 608e41f4b71Sopenharmony_ci``` 609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci### getValueAt 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_cigetValueAt(index: number): V 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci获取指定下标对应键值对中的元素。 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**参数:** 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 624e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 625e41f4b71Sopenharmony_ci| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 | 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci**返回值:** 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci| 类型 | 说明 | 630e41f4b71Sopenharmony_ci| -------- | -------- | 631e41f4b71Sopenharmony_ci| V | 返回指定下标对应键值对中的元素。 | 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci**错误码:** 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 638e41f4b71Sopenharmony_ci| -------- | -------- | 639e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 640e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 641e41f4b71Sopenharmony_ci| 10200011 | The getValueAt method cannot be bound. | 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci**示例:** 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ci```ts 646e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 647e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 648e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 649e41f4b71Sopenharmony_cilet result = lightWeightMap.getValueAt(1); 650e41f4b71Sopenharmony_ci``` 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci### clear 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_ciclear(): void 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci清除LightWeightMap中的所有元素,并把length置为0。 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**错误码:** 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 668e41f4b71Sopenharmony_ci| -------- | -------- | 669e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound. | 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**示例:** 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci```ts 674e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 675e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 676e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 677e41f4b71Sopenharmony_cilightWeightMap.clear(); 678e41f4b71Sopenharmony_ci``` 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ci### keys 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_cikeys(): IterableIterator<K> 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci返回包含此映射中包含的键的新迭代器对象。 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 688e41f4b71Sopenharmony_ci 689e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci**返回值:** 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci| 类型 | 说明 | 694e41f4b71Sopenharmony_ci| -------- | -------- | 695e41f4b71Sopenharmony_ci| IterableIterator<K> | 返回一个迭代器。 | 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ci**错误码:** 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 702e41f4b71Sopenharmony_ci| -------- | -------- | 703e41f4b71Sopenharmony_ci| 10200011 | The keys method cannot be bound. | 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci**示例:** 706e41f4b71Sopenharmony_ci 707e41f4b71Sopenharmony_ci```ts 708e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 709e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 710e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 711e41f4b71Sopenharmony_cilet iter = lightWeightMap.keys(); 712e41f4b71Sopenharmony_cilet temp: IteratorResult<string, number> = iter.next(); 713e41f4b71Sopenharmony_ciwhile(!temp.done) { 714e41f4b71Sopenharmony_ci console.log("value:" + temp.value); 715e41f4b71Sopenharmony_ci temp = iter.next(); 716e41f4b71Sopenharmony_ci} 717e41f4b71Sopenharmony_ci``` 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ci### values 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_civalues(): IterableIterator<V> 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci返回包含此映射中包含的键值的新迭代器对象。 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci**返回值:** 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci| 类型 | 说明 | 733e41f4b71Sopenharmony_ci| -------- | -------- | 734e41f4b71Sopenharmony_ci| IterableIterator<V> | 返回一个迭代器。 | 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci**错误码:** 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 741e41f4b71Sopenharmony_ci| -------- | -------- | 742e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. | 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci**示例:** 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci```ts 747e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 748e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 749e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 750e41f4b71Sopenharmony_cilet iter = lightWeightMap.values(); 751e41f4b71Sopenharmony_cilet temp: IteratorResult<number> = iter.next(); 752e41f4b71Sopenharmony_ciwhile(!temp.done) { 753e41f4b71Sopenharmony_ci console.log("value:" + temp.value); 754e41f4b71Sopenharmony_ci temp = iter.next(); 755e41f4b71Sopenharmony_ci} 756e41f4b71Sopenharmony_ci``` 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci### forEach 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_ciforEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ci通过回调函数来遍历实例对象上的元素以及元素对应的下标。 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ci**参数:** 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 772e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 773e41f4b71Sopenharmony_ci| callbackFn | function | 是 | 回调函数。 | 774e41f4b71Sopenharmony_ci| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 | 775e41f4b71Sopenharmony_ci 776e41f4b71Sopenharmony_cicallbackfn的参数说明: 777e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 778e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 779e41f4b71Sopenharmony_ci| value | V | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 | 780e41f4b71Sopenharmony_ci| key | K | 否 | 当前遍历到的元素键值对的键,默认值为首个键值对的键。 | 781e41f4b71Sopenharmony_ci| map | LightWeightMap<K, V> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 | 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci**错误码:** 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 788e41f4b71Sopenharmony_ci| -------- | -------- | 789e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 790e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. | 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci**示例:** 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci```ts 795e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 796e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 123); 797e41f4b71Sopenharmony_cilightWeightMap.set("gull", 357); 798e41f4b71Sopenharmony_cilightWeightMap.forEach((value?: number, key?: string) => { 799e41f4b71Sopenharmony_ci console.log("value:" + value, "key:" + key); 800e41f4b71Sopenharmony_ci}); 801e41f4b71Sopenharmony_ci``` 802e41f4b71Sopenharmony_ci```ts 803e41f4b71Sopenharmony_ci// 不建议在forEach中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。 804e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 805e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 806e41f4b71Sopenharmony_ci lightWeightMap.set("sparrow" + i, 123); 807e41f4b71Sopenharmony_ci} 808e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 809e41f4b71Sopenharmony_ci lightWeightMap.remove("sparrow" + i); 810e41f4b71Sopenharmony_ci} 811e41f4b71Sopenharmony_ci``` 812e41f4b71Sopenharmony_ci 813e41f4b71Sopenharmony_ci### entries 814e41f4b71Sopenharmony_ci 815e41f4b71Sopenharmony_cientries(): IterableIterator<[K, V]> 816e41f4b71Sopenharmony_ci 817e41f4b71Sopenharmony_ci返回包含此映射中包含的键值对的新迭代器对象。 818e41f4b71Sopenharmony_ci 819e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci**返回值:** 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci| 类型 | 说明 | 826e41f4b71Sopenharmony_ci| -------- | -------- | 827e41f4b71Sopenharmony_ci| IterableIterator<[K, V]> | 返回一个迭代器。 | 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci**错误码:** 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 834e41f4b71Sopenharmony_ci| -------- | -------- | 835e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound. | 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci**示例:** 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci```ts 840e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 841e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 842e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 843e41f4b71Sopenharmony_cilet iter = lightWeightMap.entries(); 844e41f4b71Sopenharmony_cilet temp: IteratorResult<Object[]> = iter.next(); 845e41f4b71Sopenharmony_ciwhile(!temp.done) { 846e41f4b71Sopenharmony_ci console.log("key:" + temp.value[0]); 847e41f4b71Sopenharmony_ci console.log("value:" + temp.value[1]); 848e41f4b71Sopenharmony_ci temp = iter.next(); 849e41f4b71Sopenharmony_ci} 850e41f4b71Sopenharmony_ci``` 851e41f4b71Sopenharmony_ci```ts 852e41f4b71Sopenharmony_ci// 不建议在entries中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。 853e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 854e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 855e41f4b71Sopenharmony_ci lightWeightMap.set("sparrow" + i, 123); 856e41f4b71Sopenharmony_ci} 857e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 858e41f4b71Sopenharmony_ci lightWeightMap.remove("sparrow" + i); 859e41f4b71Sopenharmony_ci} 860e41f4b71Sopenharmony_ci``` 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci### toString 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_citoString(): String 865e41f4b71Sopenharmony_ci 866e41f4b71Sopenharmony_ci将此映射中包含的键值对拼接成字符串,并返回字符串类型。 867e41f4b71Sopenharmony_ci 868e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 871e41f4b71Sopenharmony_ci 872e41f4b71Sopenharmony_ci**返回值:** 873e41f4b71Sopenharmony_ci 874e41f4b71Sopenharmony_ci | 类型 | 说明 | 875e41f4b71Sopenharmony_ci | -------- | -------- | 876e41f4b71Sopenharmony_ci | String | 返回一个字符串。 | 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_ci**错误码:** 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 883e41f4b71Sopenharmony_ci| -------- | -------- | 884e41f4b71Sopenharmony_ci| 10200011 | The toString method cannot be bound. | 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**示例:** 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci```ts 889e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 890e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 891e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 892e41f4b71Sopenharmony_cilet result = lightWeightMap.toString(); 893e41f4b71Sopenharmony_ci``` 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci### [Symbol.iterator] 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<[K, V]> 898e41f4b71Sopenharmony_ci 899e41f4b71Sopenharmony_ci返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 900e41f4b71Sopenharmony_ci 901e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci**返回值:** 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ci| 类型 | 说明 | 908e41f4b71Sopenharmony_ci| -------- | -------- | 909e41f4b71Sopenharmony_ci| IterableIterator<[K, V]> | 返回一个迭代器。 | 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci**错误码:** 912e41f4b71Sopenharmony_ci 913e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 916e41f4b71Sopenharmony_ci| -------- | -------- | 917e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 918e41f4b71Sopenharmony_ci 919e41f4b71Sopenharmony_ci**示例:** 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci```ts 922e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 923e41f4b71Sopenharmony_cilightWeightMap.set("squirrel", 123); 924e41f4b71Sopenharmony_cilightWeightMap.set("sparrow", 356); 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci// 使用方法一: 927e41f4b71Sopenharmony_cilet nums = Array.from(lightWeightMap.values()); 928e41f4b71Sopenharmony_cifor (let item1 of nums) { 929e41f4b71Sopenharmony_ci console.log("value:" + item1); 930e41f4b71Sopenharmony_ci} 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_cilet key = Array.from(lightWeightMap.keys()); 933e41f4b71Sopenharmony_cifor (let item2 of key) { 934e41f4b71Sopenharmony_ci console.log("key:" + item2); 935e41f4b71Sopenharmony_ci} 936e41f4b71Sopenharmony_ci 937e41f4b71Sopenharmony_ci// 使用方法二: 938e41f4b71Sopenharmony_cilet iter = lightWeightMap[Symbol.iterator](); 939e41f4b71Sopenharmony_cilet temp: IteratorResult<Object[]> = iter.next(); 940e41f4b71Sopenharmony_ciwhile(!temp.done) { 941e41f4b71Sopenharmony_ci console.log("key:" + temp.value[0]); 942e41f4b71Sopenharmony_ci console.log("value:" + temp.value[1]); 943e41f4b71Sopenharmony_ci temp = iter.next(); 944e41f4b71Sopenharmony_ci} 945e41f4b71Sopenharmony_ci``` 946e41f4b71Sopenharmony_ci```ts 947e41f4b71Sopenharmony_ci// 不建议在Symbol.iterator中使用set、setValueAt、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。 948e41f4b71Sopenharmony_cilet lightWeightMap: LightWeightMap<string, number> = new LightWeightMap(); 949e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 950e41f4b71Sopenharmony_ci lightWeightMap.set("sparrow" + i, 123); 951e41f4b71Sopenharmony_ci} 952e41f4b71Sopenharmony_cifor(let i = 0; i < 10; i++) { 953e41f4b71Sopenharmony_ci lightWeightMap.remove("sparrow" + i); 954e41f4b71Sopenharmony_ci} 955e41f4b71Sopenharmony_ci```