1# Nonlinear Container LightWeightSet 2 3> **NOTE** 4> 5> 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. 6 7**LightWeightSet** stores a set of values, each of which must be unique. 8 9**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. 10 11The values in such a set are searched using hash values, which are stored in an array. 12 13Compared with **[HashSet](js-apis-hashset.md)**, which can also store values, **LightWeightSet** occupies less memory. 14 15**Recommended use case**: Use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set. 16 17## Modules to Import 18 19```ts 20import LightWeightSet from '@ohos.util.LightWeightSet'; 21``` 22 23 24 25## LightWeightSet 26 27### Attributes 28 29**System capability**: SystemCapability.Utils.Lang 30 31| Name| Type| Readable| Writable| Description| 32| -------- | -------- | -------- | -------- | -------- | 33| length | number | Yes| No| Number of elements in a lightweight set (called container later).| 34 35 36### constructor 37 38constructor() 39 40A constructor used to create a **LightWeightSet** instance. 41 42**System capability**: SystemCapability.Utils.Lang 43 44**Example** 45 46```ts 47let lightWeightSet = new LightWeightSet(); 48``` 49 50 51### isEmpty 52 53isEmpty(): boolean 54 55Checks whether this container is empty (contains no element). 56 57**System capability**: SystemCapability.Utils.Lang 58 59**Return value** 60 61| Type| Description| 62| -------- | -------- | 63| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 64 65**Example** 66 67```ts 68const lightWeightSet = new LightWeightSet(); 69let result = lightWeightSet.isEmpty(); 70``` 71 72### add 73 74add(obj: T): boolean 75 76Adds an element to this container. 77 78**System capability**: SystemCapability.Utils.Lang 79 80**Parameters** 81 82| Name| Type| Mandatory| Description| 83| -------- | -------- | -------- | -------- | 84| obj | T | Yes| Target element.| 85 86**Return value** 87 88| Type| Description| 89| -------- | -------- | 90| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 91 92**Example** 93 94```ts 95let lightWeightSet = new LightWeightSet(); 96let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 97``` 98 99 100### addAll 101 102addAll(set: LightWeightSet<T>): boolean 103 104Adds all elements in a **LightWeightSet** instance to this container. 105 106**System capability**: SystemCapability.Utils.Lang 107 108**Parameters** 109 110| Name| Type| Mandatory| Description| 111| -------- | -------- | -------- | -------- | 112| set | LightWeightSet<T> | Yes| **LightWeightSet** instance whose elements are to be added to the current container.| 113 114**Example** 115 116```ts 117let lightWeightSet = new LightWeightSet(); 118lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 119lightWeightSet.add("sdfs"); 120let set = new LightWeightSet(); 121set.add("sfage"); 122let result = lightWeightSet.addAll(set); 123``` 124 125 126### hasAll 127 128hasAll(set: LightWeightSet<T>): boolean 129 130Checks whether this container contains all elements of the specified **LightWeightSet** instance. 131 132**System capability**: SystemCapability.Utils.Lang 133 134**Parameters** 135 136| Name| Type| Mandatory| Description| 137| -------- | -------- | -------- | -------- | 138| set | LightWeightSet<T> | Yes| **LightWeightSet** instance to be used for comparison.| 139 140**Return value** 141 142| Type| Description| 143| -------- | -------- | 144| boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.| 145 146**Example** 147 148```ts 149let lightWeightSet = new LightWeightSet(); 150lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 151lightWeightSet.add("sdfs"); 152let set = new LightWeightSet(); 153set.add("sdfs"); 154let result = lightWeightSet.hasAll(set); 155``` 156 157 158### has 159 160has(key: T): boolean 161 162Checks whether this container has the specified key. 163 164**System capability**: SystemCapability.Utils.Lang 165 166**Parameters** 167 168| Name| Type| Mandatory| Description| 169| -------- | -------- | -------- | -------- | 170| key| T | Yes| Target key.| 171 172**Return value** 173 174| Type| Description| 175| -------- | -------- | 176| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.| 177 178**Example** 179 180```ts 181let lightWeightSet = new LightWeightSet(); 182let result = lightWeightSet.has(123); 183lightWeightSet.add(123); 184result = lightWeightSet.has(123); 185``` 186 187 188### equal 189 190equal(obj: Object): boolean 191 192Checks whether this container contains objects of the same type as the specified **obj**. 193 194**System capability**: SystemCapability.Utils.Lang 195 196**Parameters** 197 198| Name| Type| Mandatory| Description| 199| -------- | -------- | -------- | -------- | 200| obj | Object | Yes| Object to be used for comparison.| 201 202**Return value** 203 204| Type| Description| 205| -------- | -------- | 206| boolean | Returns **true** if the container contains objects of the same type as the specified **obj**; returns **false** otherwise.| 207 208**Example** 209 210```ts 211let lightWeightSet = new LightWeightSet(); 212lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 213lightWeightSet.add("sdfs"); 214let obj = ["Ahfbrgrbgnutfodgorrogorgrogofdfdf", "sdfs"]; 215let result = lightWeightSet.equal(obj); 216``` 217 218 219### increaseCapacityTo 220 221increaseCapacityTo(minimumCapacity: number): void 222 223Increases the capacity of this container. 224 225**System capability**: SystemCapability.Utils.Lang 226 227**Parameters** 228 229| Name| Type| Mandatory| Description| 230| -------- | -------- | -------- | -------- | 231| minimumCapacity | number | Yes| Minimum number of elements to accommodate in the container.| 232 233**Example** 234 235```ts 236let lightWeightSet = new LightWeightSet(); 237lightWeightSet.increaseCapacityTo(10); 238``` 239 240 241### getIndexOf 242 243getIndexOf(key: T): number 244 245Obtains the position index of the element with the specified key in this container. 246 247**System capability**: SystemCapability.Utils.Lang 248 249**Parameters** 250 251| Name| Type| Mandatory| Description| 252| -------- | -------- | -------- | -------- | 253| key| T | Yes| Key of the target element.| 254 255**Return value** 256 257| Type| Description| 258| -------- | -------- | 259| number | Position index of the element.| 260 261**Example** 262 263```ts 264let lightWeightSet = new LightWeightSet(); 265lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 266lightWeightSet.add("sdfs"); 267let result = lightWeightSet.getIndexOf("sdfs"); 268``` 269 270 271### remove 272 273remove(key: T): T 274 275Removes an element of the specified key from this container. 276 277**System capability**: SystemCapability.Utils.Lang 278 279**Parameters** 280 281| Name| Type| Mandatory| Description| 282| -------- | -------- | -------- | -------- | 283| key| T | Yes| Key of the target element.| 284 285**Return value** 286 287| Type| Description| 288| -------- | -------- | 289| T | Value of the element removed.| 290 291**Example** 292 293```ts 294let lightWeightSet = new LightWeightSet(); 295lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 296lightWeightSet.add("sdfs"); 297let result = lightWeightSet.remove("sdfs"); 298``` 299 300 301### removeAt 302 303removeAt(index: number): boolean 304 305Removes the element at the specified position from this container. 306 307**System capability**: SystemCapability.Utils.Lang 308 309**Parameters** 310 311| Name| Type| Mandatory| Description| 312| -------- | -------- | -------- | -------- | 313| index | number | Yes| Position index of the element.| 314 315**Return value** 316 317| Type| Description| 318| -------- | -------- | 319| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 320 321**Example** 322 323```ts 324let lightWeightSet = new LightWeightSet(); 325lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 326lightWeightSet.add("sdfs"); 327let result = lightWeightSet.removeAt(1); 328``` 329 330 331### getValueAt 332 333getValueAt(index: number): T 334 335Obtains the value of the element at the specified position in this container. 336 337**System capability**: SystemCapability.Utils.Lang 338 339**Parameters** 340 341| Name| Type| Mandatory| Description| 342| -------- | -------- | -------- | -------- | 343| index | number | Yes| Position index of the element.| 344 345**Return value** 346 347| Type| Description| 348| -------- | -------- | 349| T | Value obtained.| 350 351**Parameters** 352 353```ts 354let lightWeightSet = new LightWeightSet(); 355lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 356lightWeightSet.add("sdfs"); 357let result = lightWeightSet.getValueAt(1); 358``` 359 360 361### clear 362 363clear(): void 364 365Clears this container and sets its length to **0**. 366 367**System capability**: SystemCapability.Utils.Lang 368 369**Example** 370 371```ts 372let lightWeightSet = new LightWeightSet(); 373lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 374lightWeightSet.add("sdfs"); 375lightWeightSet.clear(); 376``` 377 378 379### toString 380 381toString(): String 382 383Obtains a string that contains all elements in this container. 384 385**System capability**: SystemCapability.Utils.Lang 386 387**Return value** 388 389| Type| Description| 390| -------- | -------- | 391| String | String obtained.| 392 393**Example** 394 395```ts 396let lightWeightSet = new LightWeightSet(); 397lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 398lightWeightSet.add("sdfs"); 399let result = lightWeightSet.toString(); 400``` 401 402 403### toArray 404 405toArray(): Array<T> 406 407Obtains an array that contains all objects in this container. 408 409**System capability**: SystemCapability.Utils.Lang 410 411**Return value** 412 413| Type| Description| 414| -------- | -------- | 415| Array<T> | Array obtained.| 416 417**Example** 418 419```ts 420let lightWeightSet = new LightWeightSet(); 421lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 422lightWeightSet.add("sdfs"); 423let result = lightWeightSet.toArray(); 424``` 425 426 427### values 428 429values(): IterableIterator<T> 430 431Obtains an iterator that contains all the values in this container. 432 433**System capability**: SystemCapability.Utils.Lang 434 435**Return value** 436 437| Type| Description| 438| -------- | -------- | 439| IterableIterator<T> | Iterator obtained.| 440 441**Example** 442 443```ts 444let lightWeightSet = new LightWeightSet(); 445lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 446lightWeightSet.add("sdfs"); 447let iter = lightWeightSet.values(); 448let index = 0; 449while(index < lightWeightSet.length) { 450 console.log(JSON.stringify(iter.next().value)); 451 index++; 452} 453``` 454 455 456### forEach 457 458forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet<T>) => void, thisArg?: Object): void 459 460Uses a callback to traverse the elements in this container and obtain their position indexes. 461 462**System capability**: SystemCapability.Utils.Lang 463 464**Parameters** 465 466| Name| Type| Mandatory| Description| 467| -------- | -------- | -------- | -------- | 468| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.| 469| thisArg | Object | No| Value to use when the callback is invoked.| 470 471callbackfn 472| Name| Type| Mandatory| Description| 473| -------- | -------- | -------- | -------- | 474| value | T | No| Value of the element that is currently traversed.| 475| key| T | No| Key of the element that is currently traversed (same as **value**).| 476| set | LightWeightSet<T> | No| Instance that invokes the **forEach** method.| 477 478**Example** 479 480```ts 481let lightWeightSet = new LightWeightSet(); 482lightWeightSet.add("sdfs"); 483lightWeightSet.add("dfsghsf"); 484lightWeightSet.forEach((value, key) => { 485 console.log("value:" + value, "key:" + key); 486}); 487``` 488 489 490### entries 491 492entries(): IterableIterator<[T, T]> 493 494Obtains an iterator that contains all the elements in this container. 495 496**System capability**: SystemCapability.Utils.Lang 497 498**Return value** 499 500| Type| Description| 501| -------- | -------- | 502| IterableIterator<[T, T]> | Iterator obtained.| 503 504**Example** 505 506```ts 507let lightWeightSet = new LightWeightSet(); 508lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 509lightWeightSet.add("sdfs"); 510let iter = lightWeightSet.entries(); 511let index = 0; 512while(index < lightWeightSet.length) { 513 console.log(JSON.stringify(iter.next().value)); 514 index++; 515} 516``` 517 518 519### [Symbol.iterator] 520 521[Symbol.iterator]\(): IterableIterator<T> 522 523Obtains an iterator, each item of which is a JavaScript object. 524 525**System capability**: SystemCapability.Utils.Lang 526 527**Return value** 528 529| Type| Description| 530| -------- | -------- | 531| IterableIterator<T> | Iterator obtained.| 532 533**Example** 534 535```ts 536let lightWeightSet = new LightWeightSet(); 537lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 538lightWeightSet.add("sdfs"); 539 540// Method 1: 541for (let item of lightWeightSet) { 542 console.log("value:" + item); 543} 544 545// Method 2: 546let iter = lightWeightSet[Symbol.iterator](); 547let temp = iter.next().value; 548while(temp != undefined) { 549 console.log("value:" + temp); 550 temp = iter.next().value; 551} 552``` 553