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