js-apis-lightweightset.md 15.8 KB
Newer Older
G
Gloria 已提交
1
# @ohos.util.LightWeightSet (Nonlinear Container LightWeightSet)
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4 5 6 7 8 9 10 11
**LightWeightSet** stores a set of values, each of which must be unique.

**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.

The values in such a set are searched using hash values, which are stored in an array.

Compared with **[HashSet](js-apis-hashset.md)**, which can also store values, **LightWeightSet** occupies less memory.

**Recommended use case**: Use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set.
W
wusongqing 已提交
12

G
Gloria 已提交
13 14 15
This topic uses the following to identify the use of generics:
- T: Type

G
Gloria 已提交
16 17 18 19 20
> **NOTE**
>
> 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.


W
wusongqing 已提交
21 22
## Modules to Import

W
wusongqing 已提交
23
```ts
W
wusongqing 已提交
24
import LightWeightSet from '@ohos.util.LightWeightSet';  
W
wusongqing 已提交
25 26 27 28 29 30
```

## LightWeightSet

### Attributes

W
wusongqing 已提交
31 32
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
33 34
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
35
| length | number | Yes| No| Number of elements in a lightweight set (called container later).|
W
wusongqing 已提交
36 37 38 39 40 41 42 43


### constructor

constructor()

A constructor used to create a **LightWeightSet** instance.

W
wusongqing 已提交
44 45
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
46 47
**Error codes**

48
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
49 50 51 52 53

| ID| Error Message|
| -------- | -------- |
| 10200012 | The LightWeightSet's constructor cannot be directly invoked. |

W
wusongqing 已提交
54 55
**Example**

W
wusongqing 已提交
56
```ts
W
wusongqing 已提交
57 58 59 60 61 62 63 64
let lightWeightSet = new LightWeightSet();
```


### isEmpty

isEmpty(): boolean

W
wusongqing 已提交
65 66 67
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
68 69 70 71 72 73 74

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

G
Gloria 已提交
75 76
**Error codes**

77
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
78 79 80 81 82

| ID| Error Message|
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

W
wusongqing 已提交
83 84
**Example**

W
wusongqing 已提交
85
```ts
W
wusongqing 已提交
86 87 88 89 90 91 92 93
const lightWeightSet = new LightWeightSet();
let result = lightWeightSet.isEmpty();
```

### add

add(obj: T): boolean

W
wusongqing 已提交
94 95 96
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
97 98 99 100 101

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
102
| obj | T | Yes| Target element.|
W
wusongqing 已提交
103 104 105 106 107

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
108
| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
W
wusongqing 已提交
109

G
Gloria 已提交
110 111
**Error codes**

112
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
113 114 115 116 117

| ID| Error Message|
| -------- | -------- |
| 10200011 | The add method cannot be bound. |

W
wusongqing 已提交
118 119
**Example**

W
wusongqing 已提交
120
```ts
W
wusongqing 已提交
121
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
122
let result = lightWeightSet.add("squirrel");
W
wusongqing 已提交
123 124 125 126 127 128 129
```


### addAll

addAll(set: LightWeightSet<T>): boolean

W
wusongqing 已提交
130 131 132
Adds all elements in a **LightWeightSet** instance to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
133 134 135 136 137

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
138
| set | LightWeightSet<T> | Yes| **LightWeightSet** instance whose elements are to be added to the current container.|
W
wusongqing 已提交
139

G
Gloria 已提交
140 141
**Error codes**

142
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
143 144 145 146 147

| ID| Error Message|
| -------- | -------- |
| 10200011 | The addAll method cannot be bound. |

W
wusongqing 已提交
148 149
**Example**

W
wusongqing 已提交
150
```ts
W
wusongqing 已提交
151
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
152 153
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
154
let set = new LightWeightSet();
G
Gloria 已提交
155
set.add("gull");
W
wusongqing 已提交
156 157 158 159 160 161 162 163
let result = lightWeightSet.addAll(set);
```


### hasAll

hasAll(set: LightWeightSet<T>): boolean

W
wusongqing 已提交
164 165 166
Checks whether this container contains all elements of the specified **LightWeightSet** instance.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
167 168 169 170 171 172 173 174 175 176 177

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| set | LightWeightSet<T> | Yes| **LightWeightSet** instance to be used for comparison.|

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
178
| boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.|
W
wusongqing 已提交
179

G
Gloria 已提交
180 181
**Error codes**

182
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
183 184 185 186 187

| ID| Error Message|
| -------- | -------- |
| 10200011 | The hasAll method cannot be bound. |

W
wusongqing 已提交
188 189
**Example**

W
wusongqing 已提交
190
```ts
W
wusongqing 已提交
191
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
192 193
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
194
let set = new LightWeightSet();
G
Gloria 已提交
195
set.add("sparrow");
W
wusongqing 已提交
196 197 198 199 200 201 202 203 204 205
let result = lightWeightSet.hasAll(set);
```


### has

has(key: T): boolean

Checks whether this container has the specified key.

W
wusongqing 已提交
206 207
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
208 209 210 211
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
212
| key| T | Yes| Target key.|
W
wusongqing 已提交
213 214 215 216 217 218 219

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|

G
Gloria 已提交
220 221
**Error codes**

222
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
223 224 225 226 227

| ID| Error Message|
| -------- | -------- |
| 10200011 | The has method cannot be bound. |

W
wusongqing 已提交
228 229
**Example**

W
wusongqing 已提交
230
```ts
W
wusongqing 已提交
231 232
let lightWeightSet = new LightWeightSet();
lightWeightSet.add(123);
G
Gloria 已提交
233
let result = lightWeightSet.has(123);
W
wusongqing 已提交
234 235 236 237 238 239 240 241 242
```


### equal

equal(obj: Object): boolean

Checks whether this container contains objects of the same type as the specified **obj**.

W
wusongqing 已提交
243 244
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
245 246 247 248
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
249
| obj | Object | Yes| **LightWeightSet** instance to be used for comparison.|
W
wusongqing 已提交
250 251 252 253 254 255 256

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container contains objects of the same type as the specified **obj**; returns **false** otherwise.|

G
Gloria 已提交
257 258
**Error codes**

259
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
260 261 262 263 264

| ID| Error Message|
| -------- | -------- |
| 10200011 | The equal method cannot be bound. |

W
wusongqing 已提交
265 266
**Example**

W
wusongqing 已提交
267
```ts
W
wusongqing 已提交
268
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
269 270
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
G
Gloria 已提交
271
let obj = ["sparrow", "squirrel"];
W
wusongqing 已提交
272 273 274 275 276 277 278 279 280 281
let result = lightWeightSet.equal(obj);
```


### increaseCapacityTo

increaseCapacityTo(minimumCapacity: number): void

Increases the capacity of this container.

W
wusongqing 已提交
282 283
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
284 285 286 287
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
288
| minimumCapacity | number | Yes| Minimum number of elements to accommodate in the container.|
W
wusongqing 已提交
289

G
Gloria 已提交
290 291
**Error codes**

292
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
293 294 295 296

| ID| Error Message|
| -------- | -------- |
| 10200011 | The increaseCapacityTo method cannot be bound. |
297
| 10200001 | The value of minimumCapacity is out of range. |
G
Gloria 已提交
298

W
wusongqing 已提交
299 300
**Example**

W
wusongqing 已提交
301
```ts
W
wusongqing 已提交
302 303 304 305 306 307 308 309 310
let lightWeightSet = new LightWeightSet();
lightWeightSet.increaseCapacityTo(10);
```


### getIndexOf

getIndexOf(key: T): number

W
wusongqing 已提交
311 312 313
Obtains the position index of the element with the specified key in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
314 315 316 317 318

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
319
| key| T | Yes| Key of the target element.|
W
wusongqing 已提交
320 321 322 323 324

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
325
| number | Position index of the element.|
W
wusongqing 已提交
326

G
Gloria 已提交
327 328
**Error codes**

329
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
330 331 332 333 334

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getIndexOf method cannot be bound. |

W
wusongqing 已提交
335 336
**Example**

W
wusongqing 已提交
337
```ts
W
wusongqing 已提交
338
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
339 340 341
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
let result = lightWeightSet.getIndexOf("sparrow");
W
wusongqing 已提交
342 343 344 345 346 347 348
```


### remove

remove(key: T): T

W
wusongqing 已提交
349 350 351
Removes an element of the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
352 353 354 355 356

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
357
| key| T | Yes| Key of the target element.|
W
wusongqing 已提交
358 359 360 361 362

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
363
| T | Value of the element removed.|
W
wusongqing 已提交
364

G
Gloria 已提交
365 366
**Error codes**

367
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
368 369 370 371 372

| ID| Error Message|
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

W
wusongqing 已提交
373 374
**Example**

W
wusongqing 已提交
375
```ts
W
wusongqing 已提交
376
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
377 378 379
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
let result = lightWeightSet.remove("sparrow");
W
wusongqing 已提交
380 381 382 383 384 385 386
```


### removeAt

removeAt(index: number): boolean

W
wusongqing 已提交
387 388 389
Removes the element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
390 391 392 393 394

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
395
| index | number | Yes| Position index of the element.|
W
wusongqing 已提交
396 397 398 399 400

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
401
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
402

G
Gloria 已提交
403 404
**Error codes**

405
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
406 407 408 409 410

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeAt method cannot be bound. |

W
wusongqing 已提交
411 412
**Example**

W
wusongqing 已提交
413
```ts
W
wusongqing 已提交
414
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
415 416
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
417 418 419 420 421 422 423 424
let result = lightWeightSet.removeAt(1);
```


### getValueAt

getValueAt(index: number): T

W
wusongqing 已提交
425 426 427
Obtains the value of the element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
428 429 430 431 432

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
433
| index | number | Yes| Position index of the element.|
W
wusongqing 已提交
434 435 436 437 438 439 440

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value obtained.|

G
Gloria 已提交
441 442
**Error codes**

443
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
444 445 446 447 448

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getValueAt method cannot be bound. |

W
wusongqing 已提交
449 450
**Parameters**

W
wusongqing 已提交
451
```ts
W
wusongqing 已提交
452
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
453 454
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
455 456 457 458 459 460 461 462 463 464
let result = lightWeightSet.getValueAt(1);
```


### clear

clear(): void

Clears this container and sets its length to **0**.

W
wusongqing 已提交
465 466
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
467 468
**Error codes**

469
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
470 471 472 473 474

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

W
wusongqing 已提交
475 476
**Example**

W
wusongqing 已提交
477
```ts
W
wusongqing 已提交
478
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
479 480
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
481 482 483 484 485 486 487 488
lightWeightSet.clear();
```


### toString

toString(): String

W
wusongqing 已提交
489 490 491
Obtains a string that contains all elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
492 493 494 495 496 497 498 499 500

**Return value**

| Type| Description|
| -------- | -------- |
| String | String obtained.|

**Example**

W
wusongqing 已提交
501
```ts
W
wusongqing 已提交
502
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
503 504
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
505 506 507 508 509 510 511 512 513 514
let result = lightWeightSet.toString();
```


### toArray

toArray(): Array<T>

Obtains an array that contains all objects in this container.

W
wusongqing 已提交
515 516
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
517 518 519 520 521 522
**Return value**

| Type| Description|
| -------- | -------- |
| Array<T> | Array obtained.|

G
Gloria 已提交
523 524
**Error codes**

525
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
526 527 528 529 530

| ID| Error Message|
| -------- | -------- |
| 10200011 | The toArray method cannot be bound. |

W
wusongqing 已提交
531 532
**Example**

W
wusongqing 已提交
533
```ts
W
wusongqing 已提交
534
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
535 536
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
537 538 539 540 541 542 543 544 545 546
let result = lightWeightSet.toArray();
```


### values

values(): IterableIterator<T>

Obtains an iterator that contains all the values in this container.

W
wusongqing 已提交
547 548
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
549 550 551 552 553 554
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|

G
Gloria 已提交
555 556
**Error codes**

557
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
558 559 560 561 562

| ID| Error Message|
| -------- | -------- |
| 10200011 | The values method cannot be bound. |

W
wusongqing 已提交
563 564
**Example**

W
wusongqing 已提交
565
```ts
W
wusongqing 已提交
566
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
567 568
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
569 570 571 572 573 574 575 576 577 578 579
let iter = lightWeightSet.values();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
```


### forEach

G
Gloria 已提交
580
forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
W
wusongqing 已提交
581

W
wusongqing 已提交
582 583 584
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
585 586 587 588 589

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
590
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
G
Gloria 已提交
591
| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
W
wusongqing 已提交
592

G
Gloria 已提交
593
callbackFn
W
wusongqing 已提交
594 595
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
596 597 598
| value | T | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.|
| 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.|
| set | LightWeightSet&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
W
wusongqing 已提交
599

G
Gloria 已提交
600 601
**Error codes**

602
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
603 604 605 606 607

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
608 609
**Example**

W
wusongqing 已提交
610
```ts
W
wusongqing 已提交
611
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
612 613
lightWeightSet.add("sparrow");
lightWeightSet.add("gull");
W
wusongqing 已提交
614
lightWeightSet.forEach((value, key) => {
G
Gloria 已提交
615
    console.log("value:" + value, "key:" + key);
W
wusongqing 已提交
616 617 618 619 620 621 622 623
});
```


### entries

entries(): IterableIterator<[T, T]>

W
wusongqing 已提交
624 625 626
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
627 628 629 630 631 632 633

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<[T, T]> | Iterator obtained.|

G
Gloria 已提交
634 635
**Error codes**

636
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
637 638 639 640 641

| ID| Error Message|
| -------- | -------- |
| 10200011 | The entries method cannot be bound. |

W
wusongqing 已提交
642 643
**Example**

W
wusongqing 已提交
644
```ts
W
wusongqing 已提交
645
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
646 647
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
let iter = lightWeightSet.entries();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
```


### [Symbol.iterator]

[Symbol.iterator]\(): IterableIterator&lt;T&gt;

Obtains an iterator, each item of which is a JavaScript object.

W
wusongqing 已提交
663 664
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
665 666 667 668 669 670
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|

G
Gloria 已提交
671 672
**Error codes**

673
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
674 675 676 677 678

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
679 680
**Example**

W
wusongqing 已提交
681
```ts
W
wusongqing 已提交
682
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
683 684
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
685 686 687

// Method 1:
for (let item of lightWeightSet) { 
W
wusongqing 已提交
688
  console.log("value:" + item);
W
wusongqing 已提交
689 690 691 692 693 694
}

// Method 2:
let iter = lightWeightSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
695
  console.log("value:" + temp);
W
wusongqing 已提交
696 697 698
  temp = iter.next().value;
}
```