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