1# @ohos.util.LightWeightSet (Nonlinear Container LightWeightSet) 2 3**LightWeightSet** stores a set of values, each of which must be unique. 4 5**LightWeightSet** is based on generics and uses a lightweight structure. Its default initial capacity is 8, and it has the capacity doubled in each expansion. 6 7The values in such a set are searched using hash values, which are stored in an array. 8 9Compared with **[HashSet](js-apis-hashset.md)**, which can also store values, **LightWeightSet** occupies less memory. 10 11**Recommended use case**: Use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set. 12 13This topic uses the following to identify the use of generics: 14- T: Type 15 16> **NOTE** 17> 18> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 19 20 21## Modules to Import 22 23```ts 24import { LightWeightSet } from '@kit.ArkTS'; 25``` 26 27## LightWeightSet 28 29### Attributes 30 31**Atomic service API**: This API can be used in atomic services since API version 12. 32 33**System capability**: SystemCapability.Utils.Lang 34 35| Name| Type| Readable| Writable| Description| 36| -------- | -------- | -------- | -------- | -------- | 37| length | number | Yes| No| Number of elements in a lightweight set (called container later).| 38 39 40### constructor 41 42constructor() 43 44A constructor used to create a **LightWeightSet** instance. 45 46**Atomic service API**: This API can be used in atomic services since API version 12. 47 48**System capability**: SystemCapability.Utils.Lang 49 50**Error codes** 51 52For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 53 54| ID| Error Message| 55| -------- | -------- | 56| 10200012 | The LightWeightSet's constructor cannot be directly invoked. | 57 58**Example** 59 60```ts 61let lightWeightSet: LightWeightSet<number | string> = new LightWeightSet(); 62``` 63 64 65### isEmpty 66 67isEmpty(): boolean 68 69Checks whether this container is empty (contains no element). 70 71**Atomic service API**: This API can be used in atomic services since API version 12. 72 73**System capability**: SystemCapability.Utils.Lang 74 75**Return value** 76 77| Type| Description| 78| -------- | -------- | 79| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 80 81**Error codes** 82 83For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 84 85| ID| Error Message| 86| -------- | -------- | 87| 10200011 | The isEmpty method cannot be bound. | 88 89**Example** 90 91```ts 92const lightWeightSet: LightWeightSet<number> = new LightWeightSet(); 93let result = lightWeightSet.isEmpty(); 94``` 95 96### add 97 98add(obj: T): boolean 99 100Adds an element to this container. 101 102**Atomic service API**: This API can be used in atomic services since API version 12. 103 104**System capability**: SystemCapability.Utils.Lang 105 106**Parameters** 107 108| Name| Type| Mandatory| Description| 109| -------- | -------- | -------- | -------- | 110| obj | T | Yes| Target element.| 111 112**Return value** 113 114| Type| Description| 115| -------- | -------- | 116| boolean | Returns **true** if the element is added; returns **false** otherwise.| 117 118**Error codes** 119 120For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 121 122| ID| Error Message| 123| -------- | -------- | 124| 10200011 | The add method cannot be bound. | 125 126**Example** 127 128```ts 129let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 130let result = lightWeightSet.add("squirrel"); 131``` 132 133 134### addAll 135 136addAll(set: LightWeightSet<T>): boolean 137 138Adds all elements in a **LightWeightSet** instance to this container. 139 140**Atomic service API**: This API can be used in atomic services since API version 12. 141 142**System capability**: SystemCapability.Utils.Lang 143 144**Parameters** 145 146| Name| Type| Mandatory| Description| 147| -------- | -------- | -------- | -------- | 148| set | LightWeightSet<T> | Yes| **LightWeightSet** instance whose elements are to be added to the current container.| 149 150**Return value** 151 152| Type| Description| 153| -------- | -------- | 154| boolean | Returns **true** if the element is added; returns **false** otherwise.| 155 156**Error codes** 157 158For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 159 160| ID| Error Message| 161| -------- | -------- | 162| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 163| 10200011 | The addAll method cannot be bound. | 164 165**Example** 166 167```ts 168let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 169lightWeightSet.add("squirrel"); 170lightWeightSet.add("sparrow"); 171let set: LightWeightSet<string> = new LightWeightSet(); 172set.add("gull"); 173let result = lightWeightSet.addAll(set); 174``` 175 176 177### hasAll 178 179hasAll(set: LightWeightSet<T>): boolean 180 181Checks whether this container contains all elements of the specified **LightWeightSet** instance. 182 183**Atomic service API**: This API can be used in atomic services since API version 12. 184 185**System capability**: SystemCapability.Utils.Lang 186 187**Parameters** 188 189| Name| Type| Mandatory| Description| 190| -------- | -------- | -------- | -------- | 191| set | LightWeightSet<T> | Yes| **LightWeightSet** instance to be used for comparison.| 192 193**Return value** 194 195| Type| Description| 196| -------- | -------- | 197| boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.| 198 199**Error codes** 200 201For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 202 203| ID| Error Message| 204| -------- | -------- | 205| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 206| 10200011 | The hasAll method cannot be bound. | 207 208**Example** 209 210```ts 211let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 212lightWeightSet.add("squirrel"); 213lightWeightSet.add("sparrow"); 214let set: LightWeightSet<string> = new LightWeightSet(); 215set.add("sparrow"); 216let result = lightWeightSet.hasAll(set); 217``` 218 219 220### has 221 222has(key: T): boolean 223 224Checks whether this container has the specified key. 225 226**Atomic service API**: This API can be used in atomic services since API version 12. 227 228**System capability**: SystemCapability.Utils.Lang 229 230**Parameters** 231 232| Name| Type| Mandatory| Description| 233| -------- | -------- | -------- | -------- | 234| key| T | Yes| Target key.| 235 236**Return value** 237 238| Type| Description| 239| -------- | -------- | 240| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.| 241 242**Error codes** 243 244For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 245 246| ID| Error Message| 247| -------- | -------- | 248| 10200011 | The has method cannot be bound. | 249 250**Example** 251 252```ts 253let lightWeightSet: LightWeightSet<number> = new LightWeightSet(); 254lightWeightSet.add(123); 255let result = lightWeightSet.has(123); 256``` 257 258 259### increaseCapacityTo 260 261increaseCapacityTo(minimumCapacity: number): void 262 263Increases the capacity of this container. 264 265If the passed-in capacity is greater than or equal to the number of elements in this container, the container capacity is changed to the new capacity. If the passed-in capacity is less than the number of elements in this container, the capacity is not changed. 266 267**Atomic service API**: This API can be used in atomic services since API version 12. 268 269**System capability**: SystemCapability.Utils.Lang 270 271**Parameters** 272 273| Name| Type| Mandatory| Description| 274| -------- | -------- | -------- | -------- | 275| minimumCapacity | number | Yes| Minimum number of elements to accommodate in this container.| 276 277**Error codes** 278 279For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 280 281| ID| Error Message| 282| -------- | -------- | 283| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 284| 10200001 | The value of minimumCapacity is out of range. | 285| 10200011 | The increaseCapacityTo method cannot be bound. | 286 287**Example** 288 289```ts 290let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 291lightWeightSet.increaseCapacityTo(10); 292``` 293 294 295### getIndexOf 296 297getIndexOf(key: T): number 298 299Obtains the position index of the element with the specified key in this container. 300 301**Atomic service API**: This API can be used in atomic services since API version 12. 302 303**System capability**: SystemCapability.Utils.Lang 304 305**Parameters** 306 307| Name| Type| Mandatory| Description| 308| -------- | -------- | -------- | -------- | 309| key| T | Yes| Key of the target element.| 310 311**Return value** 312 313| Type| Description| 314| -------- | -------- | 315| number | Position index of the element. If the element does not exist, a negative value is returned. The negative value consists of a minus sign and the position where the element (if available) should be. The position starts from 1.| 316 317**Error codes** 318 319For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 320 321| ID| Error Message| 322| -------- | -------- | 323| 10200011 | The getIndexOf method cannot be bound. | 324 325**Example** 326 327```ts 328let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 329lightWeightSet.add("squirrel"); 330lightWeightSet.add("sparrow"); 331let result = lightWeightSet.getIndexOf("sparrow"); 332``` 333 334 335### remove 336 337remove(key: T): T 338 339Removes an element of the specified key from this container. 340 341**Atomic service API**: This API can be used in atomic services since API version 12. 342 343**System capability**: SystemCapability.Utils.Lang 344 345**Parameters** 346 347| Name| Type| Mandatory| Description| 348| -------- | -------- | -------- | -------- | 349| key| T | Yes| Key of the target element.| 350 351**Return value** 352 353| Type| Description| 354| -------- | -------- | 355| T | Value of the element removed.| 356 357**Error codes** 358 359For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 360 361| ID| Error Message| 362| -------- | -------- | 363| 10200011 | The remove method cannot be bound. | 364 365**Example** 366 367```ts 368let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 369lightWeightSet.add("squirrel"); 370lightWeightSet.add("sparrow"); 371let result = lightWeightSet.remove("sparrow"); 372``` 373 374 375### removeAt 376 377removeAt(index: number): boolean 378 379Removes the element at the specified position from this container. 380 381**Atomic service API**: This API can be used in atomic services since API version 12. 382 383**System capability**: SystemCapability.Utils.Lang 384 385**Parameters** 386 387| Name| Type| Mandatory| Description| 388| -------- | -------- | -------- | -------- | 389| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 390 391**Return value** 392 393| Type| Description| 394| -------- | -------- | 395| boolean | Returns **true** if the element is removed; returns **false** otherwise.| 396 397**Error codes** 398 399For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 400 401| ID| Error Message| 402| -------- | -------- | 403| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 404| 10200011 | The removeAt method cannot be bound. | 405 406**Example** 407 408```ts 409let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 410lightWeightSet.add("squirrel"); 411lightWeightSet.add("sparrow"); 412let result = lightWeightSet.removeAt(1); 413``` 414 415 416### getValueAt 417 418getValueAt(index: number): T 419 420Obtains the value of the element at the specified position in this container. 421 422**Atomic service API**: This API can be used in atomic services since API version 12. 423 424**System capability**: SystemCapability.Utils.Lang 425 426**Parameters** 427 428| Name| Type| Mandatory| Description| 429| -------- | -------- | -------- | -------- | 430| index | number | Yes| Position index of the element. The value must be less than or equal to int32_max, that is, 2147483647.| 431 432**Return value** 433 434| Type| Description| 435| -------- | -------- | 436| T | Value obtained.| 437 438**Error codes** 439 440For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 441 442| ID| Error Message| 443| -------- | -------- | 444| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 445| 10200011 | The getValueAt method cannot be bound. | 446 447**Parameters** 448 449```ts 450let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 451lightWeightSet.add("squirrel"); 452lightWeightSet.add("sparrow"); 453let result = lightWeightSet.getValueAt(1); 454``` 455 456 457### clear 458 459clear(): void 460 461Clears this container and sets its length to **0**. 462 463**Atomic service API**: This API can be used in atomic services since API version 12. 464 465**System capability**: SystemCapability.Utils.Lang 466 467**Error codes** 468 469For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 470 471| ID| Error Message| 472| -------- | -------- | 473| 10200011 | The clear method cannot be bound. | 474 475**Example** 476 477```ts 478let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 479lightWeightSet.add("squirrel"); 480lightWeightSet.add("sparrow"); 481lightWeightSet.clear(); 482``` 483 484 485### toString 486 487toString(): String 488 489Obtains a string that contains all elements in this container. 490 491**Atomic service API**: This API can be used in atomic services since API version 12. 492 493**System capability**: SystemCapability.Utils.Lang 494 495**Return value** 496 497| Type| Description| 498| -------- | -------- | 499| String | String obtained.| 500 501**Example** 502 503```ts 504let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 505lightWeightSet.add("squirrel"); 506lightWeightSet.add("sparrow"); 507let result = lightWeightSet.toString(); 508``` 509 510 511### toArray 512 513toArray(): Array<T> 514 515Obtains an array that contains all objects in this container. 516 517**Atomic service API**: This API can be used in atomic services since API version 12. 518 519**System capability**: SystemCapability.Utils.Lang 520 521**Return value** 522 523| Type| Description| 524| -------- | -------- | 525| Array<T> | Array obtained.| 526 527**Error codes** 528 529For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 530 531| ID| Error Message| 532| -------- | -------- | 533| 10200011 | The toArray method cannot be bound. | 534 535**Example** 536 537```ts 538let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 539lightWeightSet.add("squirrel"); 540lightWeightSet.add("sparrow"); 541let result = lightWeightSet.toArray(); 542``` 543 544 545### values 546 547values(): IterableIterator<T> 548 549Obtains an iterator that contains all the values in this container. 550 551**Atomic service API**: This API can be used in atomic services since API version 12. 552 553**System capability**: SystemCapability.Utils.Lang 554 555**Return value** 556 557| Type| Description| 558| -------- | -------- | 559| IterableIterator<T> | Iterator obtained.| 560 561**Error codes** 562 563For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 564 565| ID| Error Message| 566| -------- | -------- | 567| 10200011 | The values method cannot be bound. | 568 569**Example** 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 588Uses a callback to traverse the elements in this container and obtain their position indexes. 589 590**Atomic service API**: This API can be used in atomic services since API version 12. 591 592**System capability**: SystemCapability.Utils.Lang 593 594**Parameters** 595 596| Name| Type| Mandatory| Description| 597| -------- | -------- | -------- | -------- | 598| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 599| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 600 601callbackFn 602| Name| Type| Mandatory| Description| 603| -------- | -------- | -------- | -------- | 604| value | T | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.| 605| key| T | No| Key of the element that is currently traversed (same as **value**). The default value is the key of the first key-value pair.| 606| set | LightWeightSet<T> | No| Instance that calls the **forEach** API. The default value is this instance.| 607 608**Error codes** 609 610For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 611 612| ID| Error Message| 613| -------- | -------- | 614| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 615| 10200011 | The forEach method cannot be bound. | 616 617**Example** 618 619```ts 620let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 621lightWeightSet.add("sparrow"); 622lightWeightSet.add("gull"); 623lightWeightSet.forEach((value ?: string, key ?: string) => { 624 console.log("value:" + value, "key:" + key); 625}); 626``` 627```ts 628// You are not advised to use the add, remove, or removeAt APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 629let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 630for(let i = 0; i < 10; i++) { 631 lightWeightSet.add(i + "123"); 632} 633for(let i = 0; i < 10; i++) { 634 lightWeightSet.remove(i + "123"); 635} 636``` 637 638### entries 639 640entries(): IterableIterator<[T, T]> 641 642Obtains an iterator that contains all the elements in this container. 643 644**Atomic service API**: This API can be used in atomic services since API version 12. 645 646**System capability**: SystemCapability.Utils.Lang 647 648**Return value** 649 650| Type| Description| 651| -------- | -------- | 652| IterableIterator<[T, T]> | Iterator obtained.| 653 654**Error codes** 655 656For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 657 658| ID| Error Message| 659| -------- | -------- | 660| 10200011 | The entries method cannot be bound. | 661 662**Example** 663 664```ts 665let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 666lightWeightSet.add("squirrel"); 667lightWeightSet.add("sparrow"); 668let iter = lightWeightSet.entries(); 669let index = 0; 670while(index < lightWeightSet.length) { 671 console.log(JSON.stringify(iter.next().value)); 672 index++; 673} 674``` 675```ts 676// You are not advised to use the add, remove, or removeAt APIs in entries because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 677let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 678for(let i = 0; i < 10; i++) { 679 lightWeightSet.add(i + "123"); 680} 681for(let i = 0; i < 10; i++) { 682 lightWeightSet.remove(i + "123"); 683} 684``` 685 686### [Symbol.iterator] 687 688[Symbol.iterator]\(): IterableIterator<T> 689 690Obtains an iterator, each item of which is a JavaScript object. 691 692**Atomic service API**: This API can be used in atomic services since API version 12. 693 694**System capability**: SystemCapability.Utils.Lang 695 696**Return value** 697 698| Type| Description| 699| -------- | -------- | 700| IterableIterator<T> | Iterator obtained.| 701 702**Error codes** 703 704For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 705 706| ID| Error Message| 707| -------- | -------- | 708| 10200011 | The Symbol.iterator method cannot be bound. | 709 710**Example** 711 712```ts 713let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 714lightWeightSet.add("squirrel"); 715lightWeightSet.add("sparrow"); 716 717// Method 1: 718let nums: Array<string> = lightWeightSet.toArray() 719for (let item of nums) { 720 console.log("value:" + item); 721} 722 723// Method 2: 724let iter = lightWeightSet[Symbol.iterator](); 725let temp: IteratorResult<string> = iter.next(); 726while(!temp.done) { 727 console.log("value:" + temp.value); 728 temp = iter.next(); 729} 730``` 731```ts 732// You are not advised to use the add, remove, or removeAt APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 733let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 734for(let i = 0; i < 10; i++) { 735 lightWeightSet.add(i + "123"); 736} 737for(let i = 0; i < 10; i++) { 738 lightWeightSet.remove(i + "123"); 739} 740``` 741 742 743### equal<sup>(deprecated)</sup> 744 745equal(obj: Object): boolean 746 747Checks whether the elements of this container are the same as those of **obj**. 748 749> **NOTE** 750> 751> This API is supported since API version 10 and deprecated since API version 12. There is no substitute API. 752 753**System capability**: SystemCapability.Utils.Lang 754 755**Parameters** 756 757| Name| Type| Mandatory| Description| 758| -------- | -------- | -------- | -------- | 759| obj | Object | Yes| **LightWeightSet** instance to be used for comparison.| 760 761**Return value** 762 763| Type| Description| 764| -------- | -------- | 765| boolean | Returns **true** if **obj** is a **LightWeightSet** container or array containing only strings or numbers and the elements in them are the same; returns **false** in other cases.| 766 767**Error codes** 768 769For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 770 771| ID| Error Message| 772| -------- | -------- | 773| 10200011 | The equal method cannot be bound. | 774 775**Example** 776 777```ts 778let lightWeightSet: LightWeightSet<string> = new LightWeightSet(); 779lightWeightSet.add("squirrel"); 780lightWeightSet.add("sparrow"); 781let obj = ["sparrow", "squirrel"]; 782let result = lightWeightSet.equal(obj); 783``` 784