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