1# @ohos.util.LightWeightSet (非线性容器LightWeightSet) 2 3LightWeightSet可用于存储一系列值的集合,存储元素中value值唯一。 4 5LightWeightSet依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。 6 7集合中value值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的value值。 8 9LightWeightSet和[HashSet](js-apis-hashset.md)都是用来存储键值的集合,LightWeightSet的占用内存更小。 10 11**推荐使用场景:** 当需要存取某个集合或是对某个集合去重时,推荐使用占用内存更小的LightWeightSet。 12 13文档中存在泛型的使用,涉及以下泛型标记符:<br> 14- T:Type,类 15 16> **说明:** 17> 18> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 19 20 21## 导入模块 22 23```ts 24import LightWeightSet from '@ohos.util.LightWeightSet'; 25``` 26 27## LightWeightSet 28 29### 属性 30 31**系统能力:** SystemCapability.Utils.Lang 32 33| 名称 | 类型 | 可读 | 可写 | 说明 | 34| -------- | -------- | -------- | -------- | -------- | 35| length | number | 是 | 否 | LightWeightSet的元素个数。 | 36 37 38### constructor 39 40constructor() 41 42LightWeightSet的构造函数。 43 44**系统能力:** SystemCapability.Utils.Lang 45 46**错误码:** 47 48以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 49 50| 错误码ID | 错误信息 | 51| -------- | -------- | 52| 10200012 | The LightWeightSet's constructor cannot be directly invoked. | 53 54**示例:** 55 56```ts 57let lightWeightSet: LightWeightSet<number | string> = new LightWeightSet(); 58``` 59 60 61### isEmpty 62 63isEmpty(): boolean 64 65判断该容器是否为空。 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 lightWeightSet: LightWeightSet<number> = new LightWeightSet(); 87let result = lightWeightSet.isEmpty(); 88``` 89 90### add 91 92add(obj: T): boolean 93 94向此容器中添加数据。 95 96**系统能力:** SystemCapability.Utils.Lang 97 98**参数:** 99 100| 参数名 | 类型 | 必填 | 说明 | 101| -------- | -------- | -------- | -------- | 102| obj | T | 是 | 添加的成员数据。 | 103 104**返回值:** 105 106| 类型 | 说明 | 107| -------- | -------- | 108| boolean | 成功添加元素返回true,否则返回false。 | 109 110**错误码:** 111 112以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 113 114| 错误码ID | 错误信息 | 115| -------- | -------- | 116| 10200011 | The add method cannot be bound. | 117 118**示例:** 119 120```ts 121let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 122let result = lightWeightSet.add("squirrel"); 123``` 124 125 126### addAll 127 128addAll(set: LightWeightSet<T>): boolean 129 130将另一个容器中的所有元素组添加到当前容器中。 131 132**系统能力:** SystemCapability.Utils.Lang 133 134**参数:** 135 136| 参数名 | 类型 | 必填 | 说明 | 137| -------- | -------- | -------- | -------- | 138| set | LightWeightSet<T> | 是 | 提供添加元素的lightWeightSet。 | 139 140**返回值:** 141 142| 类型 | 说明 | 143| -------- | -------- | 144| boolean | 成功添加元素返回true,否则返回false。 | 145 146**错误码:** 147 148以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 149 150| 错误码ID | 错误信息 | 151| -------- | -------- | 152| 10200011 | The addAll method cannot be bound. | 153 154**示例:** 155 156```ts 157let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 158lightWeightSet.add("squirrel"); 159lightWeightSet.add("sparrow"); 160let set: LightWeightSet<string> = new LightWeightSet(); 161set.add("gull"); 162let result = lightWeightSet.addAll(set); 163``` 164 165 166### hasAll 167 168hasAll(set: LightWeightSet<T>): boolean 169 170判断此容器中是否含有该指定set中的所有元素。 171 172**系统能力:** SystemCapability.Utils.Lang 173 174**参数:** 175 176| 参数名 | 类型 | 必填 | 说明 | 177| -------- | -------- | -------- | -------- | 178| set | LightWeightSet<T> | 是 | 比较对象。 | 179 180**返回值:** 181 182| 类型 | 说明 | 183| -------- | -------- | 184| boolean | 包含所有元素返回true,否则返回false。 | 185 186**错误码:** 187 188以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 189 190| 错误码ID | 错误信息 | 191| -------- | -------- | 192| 10200011 | The hasAll method cannot be bound. | 193 194**示例:** 195 196```ts 197let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 198lightWeightSet.add("squirrel"); 199lightWeightSet.add("sparrow"); 200let set: LightWeightSet<string> = new LightWeightSet(); 201set.add("sparrow"); 202let result = lightWeightSet.hasAll(set); 203``` 204 205 206### has 207 208has(key: T): boolean 209 210判断此容器中是否含有该指定key。 211 212**系统能力:** SystemCapability.Utils.Lang 213 214**参数:** 215 216| 参数名 | 类型 | 必填 | 说明 | 217| -------- | -------- | -------- | -------- | 218| key | T | 是 | 指定key | 219 220**返回值:** 221 222| 类型 | 说明 | 223| -------- | -------- | 224| boolean | 包含指定key返回true,否则返回false。 | 225 226**错误码:** 227 228以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 229 230| 错误码ID | 错误信息 | 231| -------- | -------- | 232| 10200011 | The has method cannot be bound. | 233 234**示例:** 235 236```ts 237let lightWeightSet: LightWeightSet<number> = new LightWeightSet(); 238lightWeightSet.add(123); 239let result = lightWeightSet.has(123); 240``` 241 242 243### equal 244 245equal(obj: Object): boolean 246 247判断此容器中是否含有该指定obj同类型的对象。 248 249**系统能力:** SystemCapability.Utils.Lang 250 251**参数:** 252 253| 参数名 | 类型 | 必填 | 说明 | 254| -------- | -------- | -------- | -------- | 255| obj | Object | 是 | 比较对象。 | 256 257**返回值:** 258 259| 类型 | 说明 | 260| -------- | -------- | 261| boolean | 构成类型相同返回true,否则返回false。 | 262 263**错误码:** 264 265以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 266 267| 错误码ID | 错误信息 | 268| -------- | -------- | 269| 10200011 | The equal method cannot be bound. | 270 271**示例:** 272 273```ts 274let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 275lightWeightSet.add("squirrel"); 276lightWeightSet.add("sparrow"); 277let obj = ["sparrow", "squirrel"]; 278let result = lightWeightSet.equal(obj); 279``` 280 281 282### increaseCapacityTo 283 284increaseCapacityTo(minimumCapacity: number): void 285 286将当前容器扩容至可以容纳指定数量元素。 287 288**系统能力:** SystemCapability.Utils.Lang 289 290**参数:** 291 292| 参数名 | 类型 | 必填 | 说明 | 293| -------- | -------- | -------- | -------- | 294| minimumCapacity | number | 是 | 需要容纳数量。 | 295 296**错误码:** 297 298以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 299 300| 错误码ID | 错误信息 | 301| -------- | -------- | 302| 10200011 | The increaseCapacityTo method cannot be bound. | 303| 10200001 | The value of minimumCapacity is out of range. | 304 305**示例:** 306 307```ts 308let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 309lightWeightSet.increaseCapacityTo(10); 310``` 311 312 313### getIndexOf 314 315getIndexOf(key: T): number 316 317获取指定key所对应的下标。 318 319**系统能力:** SystemCapability.Utils.Lang 320 321**参数:** 322 323| 参数名 | 类型 | 必填 | 说明 | 324| -------- | -------- | -------- | -------- | 325| key | T | 是 | 查找的指定key。 | 326 327**返回值:** 328 329| 类型 | 说明 | 330| -------- | -------- | 331| number | 在lightWeightSet中指定数据的下标。若lightWeightSet中没有要查找的元素,则返回一个负值。表示目标哈希值应该插入的位置,插入位置是从1开始计数的,负号表示这是一个插入位置而不是索引。 | 332 333**错误码:** 334 335以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 336 337| 错误码ID | 错误信息 | 338| -------- | -------- | 339| 10200011 | The getIndexOf method cannot be bound. | 340 341**示例:** 342 343```ts 344let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 345lightWeightSet.add("squirrel"); 346lightWeightSet.add("sparrow"); 347let result = lightWeightSet.getIndexOf("sparrow"); 348``` 349 350 351### remove 352 353remove(key: T): T 354 355删除并返回指定key对应的元素。 356 357**系统能力:** SystemCapability.Utils.Lang 358 359**参数:** 360 361| 参数名 | 类型 | 必填 | 说明 | 362| -------- | -------- | -------- | -------- | 363| key | T | 是 | 指定key。 | 364 365**返回值:** 366 367| 类型 | 说明 | 368| -------- | -------- | 369| T | 返回删除元素的值。 | 370 371**错误码:** 372 373以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 374 375| 错误码ID | 错误信息 | 376| -------- | -------- | 377| 10200011 | The remove method cannot be bound. | 378 379**示例:** 380 381```ts 382let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 383lightWeightSet.add("squirrel"); 384lightWeightSet.add("sparrow"); 385let result = lightWeightSet.remove("sparrow"); 386``` 387 388 389### removeAt 390 391removeAt(index: number): boolean 392 393删除指定下标所对应的元素。 394 395**系统能力:** SystemCapability.Utils.Lang 396 397**参数:** 398 399| 参数名 | 类型 | 必填 | 说明 | 400| -------- | -------- | -------- | -------- | 401| index | number | 是 | 指定下标。 | 402 403**返回值:** 404 405| 类型 | 说明 | 406| -------- | -------- | 407| boolean | 确认是否成功删除元素,成功删除元素返回true,否则返回false。 | 408 409**错误码:** 410 411以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 412 413| 错误码ID | 错误信息 | 414| -------- | -------- | 415| 10200011 | The removeAt method cannot be bound. | 416 417**示例:** 418 419```ts 420let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 421lightWeightSet.add("squirrel"); 422lightWeightSet.add("sparrow"); 423let result = lightWeightSet.removeAt(1); 424``` 425 426 427### getValueAt 428 429getValueAt(index: number): T 430 431获取此容器中指定下标对应的元素。 432 433**系统能力:** SystemCapability.Utils.Lang 434 435**参数:** 436 437| 参数名 | 类型 | 必填 | 说明 | 438| -------- | -------- | -------- | -------- | 439| index | number | 是 | 指定下标。 | 440 441**返回值:** 442 443| 类型 | 说明 | 444| -------- | -------- | 445| T | 返回指定下标对应的元素。 | 446 447**错误码:** 448 449以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 450 451| 错误码ID | 错误信息 | 452| -------- | -------- | 453| 10200011 | The getValueAt method cannot be bound. | 454 455**参数:** 456 457```ts 458let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 459lightWeightSet.add("squirrel"); 460lightWeightSet.add("sparrow"); 461let result = lightWeightSet.getValueAt(1); 462``` 463 464 465### clear 466 467clear(): void 468 469清除容器中的所有元素,并把length置为0。 470 471**系统能力:** SystemCapability.Utils.Lang 472 473**错误码:** 474 475以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 476 477| 错误码ID | 错误信息 | 478| -------- | -------- | 479| 10200011 | The clear method cannot be bound. | 480 481**示例:** 482 483```ts 484let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 485lightWeightSet.add("squirrel"); 486lightWeightSet.add("sparrow"); 487lightWeightSet.clear(); 488``` 489 490 491### toString 492 493toString(): String 494 495获取包含容器中所有键和值的字符串。 496 497**系统能力:** SystemCapability.Utils.Lang 498 499**返回值:** 500 501| 类型 | 说明 | 502| -------- | -------- | 503| String | 返回对应字符串。 | 504 505**示例:** 506 507```ts 508let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 509lightWeightSet.add("squirrel"); 510lightWeightSet.add("sparrow"); 511let result = lightWeightSet.toString(); 512``` 513 514 515### toArray 516 517toArray(): Array<T> 518 519获取包含此容器中所有对象的数组。 520 521**系统能力:** SystemCapability.Utils.Lang 522 523**返回值:** 524 525| 类型 | 说明 | 526| -------- | -------- | 527| Array<T> | 返回对应数组。 | 528 529**错误码:** 530 531以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 532 533| 错误码ID | 错误信息 | 534| -------- | -------- | 535| 10200011 | The toArray method cannot be bound. | 536 537**示例:** 538 539```ts 540let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 541lightWeightSet.add("squirrel"); 542lightWeightSet.add("sparrow"); 543let result = lightWeightSet.toArray(); 544``` 545 546 547### values 548 549values(): IterableIterator<T> 550 551返回包含此映射中包含的键值的新迭代器对象。 552 553**系统能力:** SystemCapability.Utils.Lang 554 555**返回值:** 556 557| 类型 | 说明 | 558| -------- | -------- | 559| IterableIterator<T> | 返回一个迭代器。 | 560 561**错误码:** 562 563以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 564 565| 错误码ID | 错误信息 | 566| -------- | -------- | 567| 10200011 | The values method cannot be bound. | 568 569**示例:** 570 571```ts 572let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 573lightWeightSet.add("squirrel"); 574lightWeightSet.add("sparrow"); 575let iter = lightWeightSet.values(); 576let index = 0; 577while(index < lightWeightSet.length) { 578 console.log(JSON.stringify(iter.next().value)); 579 index++; 580} 581``` 582 583 584### forEach 585 586forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet<T>) => void, thisArg?: Object): void 587 588通过回调函数来遍历LightWeightSet实例对象上的元素以及元素对应的下标。 589 590**系统能力:** SystemCapability.Utils.Lang 591 592**参数:** 593 594| 参数名 | 类型 | 必填 | 说明 | 595| -------- | -------- | -------- | -------- | 596| callbackFn | function | 是 | 回调函数。 | 597| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 | 598 599callbackfn的参数说明: 600| 参数名 | 类型 | 必填 | 说明 | 601| -------- | -------- | -------- | -------- | 602| value | T | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 | 603| key | T | 否 | 当前遍历到的元素键值对的键(和value相同),默认值为首个键值对的键。 | 604| set | LightWeightSet<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 | 605 606**错误码:** 607 608以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 609 610| 错误码ID | 错误信息 | 611| -------- | -------- | 612| 10200011 | The forEach method cannot be bound. | 613 614**示例:** 615 616```ts 617let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 618lightWeightSet.add("sparrow"); 619lightWeightSet.add("gull"); 620lightWeightSet.forEach((value ?: string, key ?: string) => { 621 console.log("value:" + value, "key:" + key); 622}); 623``` 624 625 626### entries 627 628entries(): IterableIterator<[T, T]> 629 630返回包含此映射中包含的键值对的新迭代器对象。 631 632**系统能力:** SystemCapability.Utils.Lang 633 634**返回值:** 635 636| 类型 | 说明 | 637| -------- | -------- | 638| IterableIterator<[T, T]> | 返回一个迭代器。 | 639 640**错误码:** 641 642以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 643 644| 错误码ID | 错误信息 | 645| -------- | -------- | 646| 10200011 | The entries method cannot be bound. | 647 648**示例:** 649 650```ts 651let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 652lightWeightSet.add("squirrel"); 653lightWeightSet.add("sparrow"); 654let iter = lightWeightSet.entries(); 655let index = 0; 656while(index < lightWeightSet.length) { 657 console.log(JSON.stringify(iter.next().value)); 658 index++; 659} 660``` 661 662 663### [Symbol.iterator] 664 665[Symbol.iterator]\(): IterableIterator<T> 666 667返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 668 669> **说明:** 670> 671> 本接口不支持在.ets文件中使用 672 673**系统能力:** SystemCapability.Utils.Lang 674 675**返回值:** 676 677| 类型 | 说明 | 678| -------- | -------- | 679| IterableIterator<T> | 返回一个迭代器。 | 680 681**错误码:** 682 683以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 684 685| 错误码ID | 错误信息 | 686| -------- | -------- | 687| 10200011 | The Symbol.iterator method cannot be bound. | 688 689**示例:** 690 691```ts 692let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 693lightWeightSet.add("squirrel"); 694lightWeightSet.add("sparrow"); 695 696// 使用方法一: 697let nums: Array<string> = lightWeightSet.toArray() 698for (let item of nums) { 699 console.log("value:" + item); 700} 701 702// 使用方法二: 703let iter = lightWeightSet[Symbol.iterator](); 704let temp: IteratorResult<string> = iter.next(); 705while(!temp.done) { 706 console.log("value:" + temp.value); 707 temp = iter.next(); 708} 709```