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 '@ohos.util.LightWeightSet'; 25``` 26 27## LightWeightSet 28 29### Attributes 30 31**System capability**: SystemCapability.Utils.Lang 32 33| Name| Type| Readable| Writable| Description| 34| -------- | -------- | -------- | -------- | -------- | 35| length | number | Yes| No| Number of elements in a lightweight set (called container later).| 36 37 38### constructor 39 40constructor() 41 42A constructor used to create a **LightWeightSet** instance. 43 44**System capability**: SystemCapability.Utils.Lang 45 46**Error codes** 47 48For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 49 50| ID| Error Message| 51| -------- | -------- | 52| 10200012 | The LightWeightSet's constructor cannot be directly invoked. | 53 54**Example** 55 56```ts 57let lightWeightSet = new LightWeightSet(); 58``` 59 60 61### isEmpty 62 63isEmpty(): boolean 64 65Checks whether this container is empty (contains no element). 66 67**System capability**: SystemCapability.Utils.Lang 68 69**Return value** 70 71| Type| Description| 72| -------- | -------- | 73| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 74 75**Error codes** 76 77For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 78 79| ID| Error Message| 80| -------- | -------- | 81| 10200011 | The isEmpty method cannot be bound. | 82 83**Example** 84 85```ts 86const lightWeightSet = new LightWeightSet(); 87let result = lightWeightSet.isEmpty(); 88``` 89 90### add 91 92add(obj: T): boolean 93 94Adds an element to this container. 95 96**System capability**: SystemCapability.Utils.Lang 97 98**Parameters** 99 100| Name| Type| Mandatory| Description| 101| -------- | -------- | -------- | -------- | 102| obj | T | Yes| Target element.| 103 104**Return value** 105 106| Type| Description| 107| -------- | -------- | 108| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 109 110**Error codes** 111 112For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 113 114| ID| Error Message| 115| -------- | -------- | 116| 10200011 | The add method cannot be bound. | 117 118**Example** 119 120```ts 121let lightWeightSet = new LightWeightSet(); 122let result = lightWeightSet.add("squirrel"); 123``` 124 125 126### addAll 127 128addAll(set: LightWeightSet<T>): boolean 129 130Adds all elements in a **LightWeightSet** instance to this container. 131 132**System capability**: SystemCapability.Utils.Lang 133 134**Parameters** 135 136| Name| Type| Mandatory| Description| 137| -------- | -------- | -------- | -------- | 138| set | LightWeightSet<T> | Yes| **LightWeightSet** instance whose elements are to be added to the current container.| 139 140**Error codes** 141 142For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 143 144| ID| Error Message| 145| -------- | -------- | 146| 10200011 | The addAll method cannot be bound. | 147 148**Example** 149 150```ts 151let lightWeightSet = new LightWeightSet(); 152lightWeightSet.add("squirrel"); 153lightWeightSet.add("sparrow"); 154let set = new LightWeightSet(); 155set.add("gull"); 156let result = lightWeightSet.addAll(set); 157``` 158 159 160### hasAll 161 162hasAll(set: LightWeightSet<T>): boolean 163 164Checks whether this container contains all elements of the specified **LightWeightSet** instance. 165 166**System capability**: SystemCapability.Utils.Lang 167 168**Parameters** 169 170| Name| Type| Mandatory| Description| 171| -------- | -------- | -------- | -------- | 172| set | LightWeightSet<T> | Yes| **LightWeightSet** instance to be used for comparison.| 173 174**Return value** 175 176| Type| Description| 177| -------- | -------- | 178| boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.| 179 180**Error codes** 181 182For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 183 184| ID| Error Message| 185| -------- | -------- | 186| 10200011 | The hasAll method cannot be bound. | 187 188**Example** 189 190```ts 191let lightWeightSet = new LightWeightSet(); 192lightWeightSet.add("squirrel"); 193lightWeightSet.add("sparrow"); 194let set = new LightWeightSet(); 195set.add("sparrow"); 196let result = lightWeightSet.hasAll(set); 197``` 198 199 200### has 201 202has(key: T): boolean 203 204Checks whether this container has the specified key. 205 206**System capability**: SystemCapability.Utils.Lang 207 208**Parameters** 209 210| Name| Type| Mandatory| Description| 211| -------- | -------- | -------- | -------- | 212| key| T | Yes| Target key.| 213 214**Return value** 215 216| Type| Description| 217| -------- | -------- | 218| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.| 219 220**Error codes** 221 222For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 223 224| ID| Error Message| 225| -------- | -------- | 226| 10200011 | The has method cannot be bound. | 227 228**Example** 229 230```ts 231let lightWeightSet = new LightWeightSet(); 232lightWeightSet.add(123); 233let result = lightWeightSet.has(123); 234``` 235 236 237### equal 238 239equal(obj: Object): boolean 240 241Checks whether this container contains objects of the same type as the specified **obj**. 242 243**System capability**: SystemCapability.Utils.Lang 244 245**Parameters** 246 247| Name| Type| Mandatory| Description| 248| -------- | -------- | -------- | -------- | 249| obj | Object | Yes| **LightWeightSet** instance to be used for comparison.| 250 251**Return value** 252 253| Type| Description| 254| -------- | -------- | 255| boolean | Returns **true** if the container contains objects of the same type as the specified **obj**; returns **false** otherwise.| 256 257**Error codes** 258 259For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 260 261| ID| Error Message| 262| -------- | -------- | 263| 10200011 | The equal method cannot be bound. | 264 265**Example** 266 267```ts 268let lightWeightSet = new LightWeightSet(); 269lightWeightSet.add("squirrel"); 270lightWeightSet.add("sparrow"); 271let obj = ["sparrow", "squirrel"]; 272let result = lightWeightSet.equal(obj); 273``` 274 275 276### increaseCapacityTo 277 278increaseCapacityTo(minimumCapacity: number): void 279 280Increases the capacity of this container. 281 282**System capability**: SystemCapability.Utils.Lang 283 284**Parameters** 285 286| Name| Type| Mandatory| Description| 287| -------- | -------- | -------- | -------- | 288| minimumCapacity | number | Yes| Minimum number of elements to accommodate in the container.| 289 290**Error codes** 291 292For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 293 294| ID| Error Message| 295| -------- | -------- | 296| 10200011 | The increaseCapacityTo method cannot be bound. | 297| 10200001 | The value of minimumCapacity is out of range. | 298 299**Example** 300 301```ts 302let lightWeightSet = new LightWeightSet(); 303lightWeightSet.increaseCapacityTo(10); 304``` 305 306 307### getIndexOf 308 309getIndexOf(key: T): number 310 311Obtains the position index of the element with the specified key in this container. 312 313**System capability**: SystemCapability.Utils.Lang 314 315**Parameters** 316 317| Name| Type| Mandatory| Description| 318| -------- | -------- | -------- | -------- | 319| key| T | Yes| Key of the target element.| 320 321**Return value** 322 323| Type| Description| 324| -------- | -------- | 325| number | Position index of the element.| 326 327**Error codes** 328 329For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 330 331| ID| Error Message| 332| -------- | -------- | 333| 10200011 | The getIndexOf method cannot be bound. | 334 335**Example** 336 337```ts 338let lightWeightSet = new LightWeightSet(); 339lightWeightSet.add("squirrel"); 340lightWeightSet.add("sparrow"); 341let result = lightWeightSet.getIndexOf("sparrow"); 342``` 343 344 345### remove 346 347remove(key: T): T 348 349Removes an element of the specified key from this container. 350 351**System capability**: SystemCapability.Utils.Lang 352 353**Parameters** 354 355| Name| Type| Mandatory| Description| 356| -------- | -------- | -------- | -------- | 357| key| T | Yes| Key of the target element.| 358 359**Return value** 360 361| Type| Description| 362| -------- | -------- | 363| T | Value of the element removed.| 364 365**Error codes** 366 367For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 368 369| ID| Error Message| 370| -------- | -------- | 371| 10200011 | The remove method cannot be bound. | 372 373**Example** 374 375```ts 376let lightWeightSet = new LightWeightSet(); 377lightWeightSet.add("squirrel"); 378lightWeightSet.add("sparrow"); 379let result = lightWeightSet.remove("sparrow"); 380``` 381 382 383### removeAt 384 385removeAt(index: number): boolean 386 387Removes the element at the specified position from this container. 388 389**System capability**: SystemCapability.Utils.Lang 390 391**Parameters** 392 393| Name| Type| Mandatory| Description| 394| -------- | -------- | -------- | -------- | 395| index | number | Yes| Position index of the element.| 396 397**Return value** 398 399| Type| Description| 400| -------- | -------- | 401| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 402 403**Error codes** 404 405For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 406 407| ID| Error Message| 408| -------- | -------- | 409| 10200011 | The removeAt method cannot be bound. | 410 411**Example** 412 413```ts 414let lightWeightSet = new LightWeightSet(); 415lightWeightSet.add("squirrel"); 416lightWeightSet.add("sparrow"); 417let result = lightWeightSet.removeAt(1); 418``` 419 420 421### getValueAt 422 423getValueAt(index: number): T 424 425Obtains the value of the element at the specified position in this container. 426 427**System capability**: SystemCapability.Utils.Lang 428 429**Parameters** 430 431| Name| Type| Mandatory| Description| 432| -------- | -------- | -------- | -------- | 433| index | number | Yes| Position index of the element.| 434 435**Return value** 436 437| Type| Description| 438| -------- | -------- | 439| T | Value obtained.| 440 441**Error codes** 442 443For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 444 445| ID| Error Message| 446| -------- | -------- | 447| 10200011 | The getValueAt method cannot be bound. | 448 449**Parameters** 450 451```ts 452let lightWeightSet = new LightWeightSet(); 453lightWeightSet.add("squirrel"); 454lightWeightSet.add("sparrow"); 455let result = lightWeightSet.getValueAt(1); 456``` 457 458 459### clear 460 461clear(): void 462 463Clears this container and sets its length to **0**. 464 465**System capability**: SystemCapability.Utils.Lang 466 467**Error codes** 468 469For details about the error codes, see [Utils Error Codes](../errorcodes/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 = 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**System capability**: SystemCapability.Utils.Lang 492 493**Return value** 494 495| Type| Description| 496| -------- | -------- | 497| String | String obtained.| 498 499**Example** 500 501```ts 502let lightWeightSet = new LightWeightSet(); 503lightWeightSet.add("squirrel"); 504lightWeightSet.add("sparrow"); 505let result = lightWeightSet.toString(); 506``` 507 508 509### toArray 510 511toArray(): Array<T> 512 513Obtains an array that contains all objects in this container. 514 515**System capability**: SystemCapability.Utils.Lang 516 517**Return value** 518 519| Type| Description| 520| -------- | -------- | 521| Array<T> | Array obtained.| 522 523**Error codes** 524 525For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 526 527| ID| Error Message| 528| -------- | -------- | 529| 10200011 | The toArray method cannot be bound. | 530 531**Example** 532 533```ts 534let lightWeightSet = new LightWeightSet(); 535lightWeightSet.add("squirrel"); 536lightWeightSet.add("sparrow"); 537let result = lightWeightSet.toArray(); 538``` 539 540 541### values 542 543values(): IterableIterator<T> 544 545Obtains an iterator that contains all the values in this container. 546 547**System capability**: SystemCapability.Utils.Lang 548 549**Return value** 550 551| Type| Description| 552| -------- | -------- | 553| IterableIterator<T> | Iterator obtained.| 554 555**Error codes** 556 557For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 558 559| ID| Error Message| 560| -------- | -------- | 561| 10200011 | The values method cannot be bound. | 562 563**Example** 564 565```ts 566let lightWeightSet = new LightWeightSet(); 567lightWeightSet.add("squirrel"); 568lightWeightSet.add("sparrow"); 569let iter = lightWeightSet.values(); 570let index = 0; 571while(index < lightWeightSet.length) { 572 console.log(JSON.stringify(iter.next().value)); 573 index++; 574} 575``` 576 577 578### forEach 579 580forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet<T>) => void, thisArg?: Object): void 581 582Uses a callback to traverse the elements in this container and obtain their position indexes. 583 584**System capability**: SystemCapability.Utils.Lang 585 586**Parameters** 587 588| Name| Type| Mandatory| Description| 589| -------- | -------- | -------- | -------- | 590| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 591| thisArg | Object | No| Value to use when the callback is invoked.| 592 593callbackfn 594| Name| Type| Mandatory| Description| 595| -------- | -------- | -------- | -------- | 596| value | T | No| Value of the element that is currently traversed.| 597| key| T | No| Key of the element that is currently traversed (same as **value**).| 598| set | LightWeightSet<T> | No| Instance that invokes the **forEach** method.| 599 600**Error codes** 601 602For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 603 604| ID| Error Message| 605| -------- | -------- | 606| 10200011 | The forEach method cannot be bound. | 607 608**Example** 609 610```ts 611let lightWeightSet = new LightWeightSet(); 612lightWeightSet.add("sparrow"); 613lightWeightSet.add("gull"); 614lightWeightSet.forEach((value, key) => { 615 console.log("value:" + value, "key:" + key); 616}); 617``` 618 619 620### entries 621 622entries(): IterableIterator<[T, T]> 623 624Obtains an iterator that contains all the elements in this container. 625 626**System capability**: SystemCapability.Utils.Lang 627 628**Return value** 629 630| Type| Description| 631| -------- | -------- | 632| IterableIterator<[T, T]> | Iterator obtained.| 633 634**Error codes** 635 636For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 637 638| ID| Error Message| 639| -------- | -------- | 640| 10200011 | The entries method cannot be bound. | 641 642**Example** 643 644```ts 645let lightWeightSet = new LightWeightSet(); 646lightWeightSet.add("squirrel"); 647lightWeightSet.add("sparrow"); 648let iter = lightWeightSet.entries(); 649let index = 0; 650while(index < lightWeightSet.length) { 651 console.log(JSON.stringify(iter.next().value)); 652 index++; 653} 654``` 655 656 657### [Symbol.iterator] 658 659[Symbol.iterator]\(): IterableIterator<T> 660 661Obtains an iterator, each item of which is a JavaScript object. 662 663**System capability**: SystemCapability.Utils.Lang 664 665**Return value** 666 667| Type| Description| 668| -------- | -------- | 669| IterableIterator<T> | Iterator obtained.| 670 671**Error codes** 672 673For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 674 675| ID| Error Message| 676| -------- | -------- | 677| 10200011 | The Symbol.iterator method cannot be bound. | 678 679**Example** 680 681```ts 682let lightWeightSet = new LightWeightSet(); 683lightWeightSet.add("squirrel"); 684lightWeightSet.add("sparrow"); 685 686// Method 1: 687for (let item of lightWeightSet) { 688 console.log("value:" + item); 689} 690 691// Method 2: 692let iter = lightWeightSet[Symbol.iterator](); 693let temp = iter.next().value; 694while(temp != undefined) { 695 console.log("value:" + temp); 696 temp = iter.next().value; 697} 698``` 699