js-apis-lightweightset.md 21.0 KB
Newer Older
W
wusongqing 已提交
1 2
# Nonlinear Container LightWeightSet 

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
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.
W
wusongqing 已提交
6

W
wusongqing 已提交
7 8 9 10 11 12 13 14 15
**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 已提交
16

G
Gloria 已提交
17 18 19
This topic uses the following to identify the use of generics:
- T: Type

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

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



## LightWeightSet

### Attributes

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

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


### constructor

constructor()

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

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

G
Gloria 已提交
47 48 49 50 51 52 53 54
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

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

W
wusongqing 已提交
57
```ts
W
wusongqing 已提交
58
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
59 60 61 62 63
try {
  let lightWeightSet2 = LightWeightSet();
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
64 65 66 67 68 69 70
```


### isEmpty

isEmpty(): boolean

W
wusongqing 已提交
71 72 73
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
74 75 76 77 78 79 80

**Return value**

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

G
Gloria 已提交
81 82 83 84 85 86 87 88
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
89 90
**Example**

W
wusongqing 已提交
91
```ts
W
wusongqing 已提交
92 93
const lightWeightSet = new LightWeightSet();
let result = lightWeightSet.isEmpty();
G
Gloria 已提交
94 95 96 97 98
try {
  lightWeightSet.isEmpty.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
99 100 101 102 103 104
```

### add

add(obj: T): boolean

W
wusongqing 已提交
105 106 107
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
108 109 110 111 112

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
113
| obj | T | Yes| Target element.|
W
wusongqing 已提交
114 115 116 117 118

**Return value**

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

G
Gloria 已提交
121 122 123 124 125 126 127 128
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
129 130
**Example**

W
wusongqing 已提交
131
```ts
W
wusongqing 已提交
132
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
133
let result = lightWeightSet.add("squirrel");
G
Gloria 已提交
134 135 136 137 138
try {
  lightWeightSet.add.bind({}, "squirrel")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
139 140 141 142 143 144 145
```


### addAll

addAll(set: LightWeightSet<T>): boolean

W
wusongqing 已提交
146 147 148
Adds all elements in a **LightWeightSet** instance to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
149 150 151 152 153

**Parameters**

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

G
Gloria 已提交
156 157 158 159 160 161 162 163
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
164 165
**Example**

W
wusongqing 已提交
166
```ts
W
wusongqing 已提交
167
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
168 169
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
170
let set = new LightWeightSet();
G
Gloria 已提交
171
set.add("gull");
W
wusongqing 已提交
172
let result = lightWeightSet.addAll(set);
G
Gloria 已提交
173 174 175 176 177
try {
  lightWeightSet.addAll.bind({}, set)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
178 179 180 181 182 183 184
```


### hasAll

hasAll(set: LightWeightSet<T>): boolean

W
wusongqing 已提交
185 186 187
Checks whether this container contains all elements of the specified **LightWeightSet** instance.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
188 189 190 191 192 193 194 195 196 197 198

**Parameters**

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

**Return value**

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

G
Gloria 已提交
201 202 203 204 205 206 207 208
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
209 210
**Example**

W
wusongqing 已提交
211
```ts
W
wusongqing 已提交
212
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
213 214
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
215
let set = new LightWeightSet();
G
Gloria 已提交
216
set.add("sparrow");
W
wusongqing 已提交
217
let result = lightWeightSet.hasAll(set);
G
Gloria 已提交
218 219 220 221 222
try {
  lightWeightSet.hasAll.bind({}, set)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
223 224 225 226 227 228 229 230 231
```


### has

has(key: T): boolean

Checks whether this container has the specified key.

W
wusongqing 已提交
232 233
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
234 235 236 237
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
238
| key| T | Yes| Target key.|
W
wusongqing 已提交
239 240 241 242 243 244 245

**Return value**

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

G
Gloria 已提交
246 247 248 249 250 251 252 253
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
254 255
**Example**

W
wusongqing 已提交
256
```ts
W
wusongqing 已提交
257 258 259
let lightWeightSet = new LightWeightSet();
let result = lightWeightSet.has(123);
lightWeightSet.add(123);
W
wusongqing 已提交
260
result = lightWeightSet.has(123);
G
Gloria 已提交
261 262 263 264 265
try {
  lightWeightSet.has.bind({}, 123)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
266 267 268 269 270 271 272 273 274
```


### equal

equal(obj: Object): boolean

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

W
wusongqing 已提交
275 276
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
277 278 279 280 281 282 283 284 285 286 287 288
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| obj | Object | Yes| Object to be used for comparison.|

**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 已提交
289 290 291 292 293 294 295 296
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
297 298
**Example**

W
wusongqing 已提交
299
```ts
W
wusongqing 已提交
300
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
301 302 303
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
let obj = ["squirrel", "sparrow"];
W
wusongqing 已提交
304
let result = lightWeightSet.equal(obj);
G
Gloria 已提交
305 306 307 308 309
try {
  lightWeightSet.equal.bind({}, obj)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
310 311 312 313 314 315 316 317 318
```


### increaseCapacityTo

increaseCapacityTo(minimumCapacity: number): void

Increases the capacity of this container.

W
wusongqing 已提交
319 320
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
321 322 323 324
**Parameters**

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

G
Gloria 已提交
327 328 329 330 331 332 333 334 335
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The increaseCapacityTo method cannot be bound. |
| 10200001 | The value of parameters are out of range. |

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

W
wusongqing 已提交
338
```ts
W
wusongqing 已提交
339 340
let lightWeightSet = new LightWeightSet();
lightWeightSet.increaseCapacityTo(10);
G
Gloria 已提交
341 342 343 344 345 346 347 348 349 350
try {
  lightWeightSet.increaseCapacityTo.bind({}, 10)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
try {
  lightWeightSet.increaseCapacityTo(2);
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
351 352 353 354 355 356 357
```


### getIndexOf

getIndexOf(key: T): number

W
wusongqing 已提交
358 359 360
Obtains the position index of the element with the specified key in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
361 362 363 364 365

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
366
| key| T | Yes| Key of the target element.|
W
wusongqing 已提交
367 368 369 370 371

**Return value**

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

G
Gloria 已提交
374 375 376 377 378 379 380 381
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
382 383
**Example**

W
wusongqing 已提交
384
```ts
W
wusongqing 已提交
385
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
386 387 388
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
let result = lightWeightSet.getIndexOf("sparrow");
G
Gloria 已提交
389 390 391 392 393
try {
  lightWeightSet.getIndexOf.bind({}, "sparrow")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
394 395 396 397 398 399 400
```


### remove

remove(key: T): T

W
wusongqing 已提交
401 402 403
Removes an element of the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
404 405 406 407 408

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
409
| key| T | Yes| Key of the target element.|
W
wusongqing 已提交
410 411 412 413 414

**Return value**

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

G
Gloria 已提交
417 418 419 420 421 422 423 424
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
425 426
**Example**

W
wusongqing 已提交
427
```ts
W
wusongqing 已提交
428
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
429 430 431
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
let result = lightWeightSet.remove("sparrow");
G
Gloria 已提交
432 433 434 435 436
try {
  lightWeightSet.remove.bind({}, "sparrow")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
437 438 439 440 441 442 443
```


### removeAt

removeAt(index: number): boolean

W
wusongqing 已提交
444 445 446
Removes the element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
447 448 449 450 451

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
452
| index | number | Yes| Position index of the element.|
W
wusongqing 已提交
453 454 455 456 457

**Return value**

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

G
Gloria 已提交
460 461 462 463 464 465 466 467
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
468 469
**Example**

W
wusongqing 已提交
470
```ts
W
wusongqing 已提交
471
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
472 473
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
474
let result = lightWeightSet.removeAt(1);
G
Gloria 已提交
475 476 477 478 479
try {
  lightWeightSet.removeAt.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
480 481 482 483 484 485 486
```


### getValueAt

getValueAt(index: number): T

W
wusongqing 已提交
487 488 489
Obtains the value of the element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
490 491 492 493 494

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
495
| index | number | Yes| Position index of the element.|
W
wusongqing 已提交
496 497 498 499 500 501 502

**Return value**

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

G
Gloria 已提交
503 504 505 506 507 508 509 510
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
511 512
**Parameters**

W
wusongqing 已提交
513
```ts
W
wusongqing 已提交
514
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
515 516
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
517
let result = lightWeightSet.getValueAt(1);
G
Gloria 已提交
518 519 520 521 522
try {
  lightWeightSet.getValueAt.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
523 524 525 526 527 528 529 530 531
```


### clear

clear(): void

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

W
wusongqing 已提交
532 533
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
534 535 536 537 538 539 540 541
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
542 543
**Example**

W
wusongqing 已提交
544
```ts
W
wusongqing 已提交
545
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
546 547
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
548
lightWeightSet.clear();
G
Gloria 已提交
549 550 551 552 553
try {
  lightWeightSet.clear.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
554 555 556 557 558 559 560
```


### toString

toString(): String

W
wusongqing 已提交
561 562 563
Obtains a string that contains all elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
564 565 566 567 568 569 570

**Return value**

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

G
Gloria 已提交
571 572 573 574 575 576 577 578
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
579 580
**Example**

W
wusongqing 已提交
581
```ts
W
wusongqing 已提交
582
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
583 584
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
585
let result = lightWeightSet.toString();
G
Gloria 已提交
586 587 588 589 590
try {
  lightWeightSet.toString.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
591 592 593 594 595 596 597 598 599
```


### toArray

toArray(): Array<T>

Obtains an array that contains all objects in this container.

W
wusongqing 已提交
600 601
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
602 603 604 605 606 607
**Return value**

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

G
Gloria 已提交
608 609 610 611 612 613 614 615
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
616 617
**Example**

W
wusongqing 已提交
618
```ts
W
wusongqing 已提交
619
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
620 621
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
622
let result = lightWeightSet.toArray();
G
Gloria 已提交
623 624 625 626 627
try {
  lightWeightSet.toArray.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
628 629 630 631 632 633 634 635 636
```


### values

values(): IterableIterator<T>

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

W
wusongqing 已提交
637 638
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
639 640 641 642 643 644
**Return value**

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

G
Gloria 已提交
645 646 647 648 649 650 651 652
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
653 654
**Example**

W
wusongqing 已提交
655
```ts
W
wusongqing 已提交
656
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
657 658
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
659 660 661 662 663 664
let iter = lightWeightSet.values();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
G
Gloria 已提交
665 666 667 668 669
try {
  lightWeightSet.values.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
670 671 672 673 674
```


### forEach

W
wusongqing 已提交
675
forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
W
wusongqing 已提交
676

W
wusongqing 已提交
677 678 679
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
680 681 682 683 684

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
685
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
686 687 688 689 690
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
691 692
| value | T | No| Value of the element that is currently traversed.|
| key| T | No| Key of the element that is currently traversed (same as **value**).|
W
wusongqing 已提交
693 694
| set | LightWeightSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
695 696 697 698 699 700 701 702
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
703 704
**Example**

W
wusongqing 已提交
705
```ts
W
wusongqing 已提交
706
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
707 708
lightWeightSet.add("sparrow");
lightWeightSet.add("gull");
W
wusongqing 已提交
709
lightWeightSet.forEach((value, key) => {
W
wusongqing 已提交
710
  console.log("value:" + value, key);
W
wusongqing 已提交
711
});
G
Gloria 已提交
712 713 714 715 716 717 718
try {
  lightWeightSet.forEach.bind({}, (value, key) => {
    console.log("value:" + value, key);
  })(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
719 720 721 722 723 724 725
```


### entries

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

W
wusongqing 已提交
726 727 728
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
729 730 731 732 733 734 735

**Return value**

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

G
Gloria 已提交
736 737 738 739 740 741 742 743
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
744 745
**Example**

W
wusongqing 已提交
746
```ts
W
wusongqing 已提交
747
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
748 749
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
750 751 752 753 754 755
let iter = lightWeightSet.entries();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
G
Gloria 已提交
756 757 758 759 760
try {
  lightWeightSet.entries.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
761 762 763 764 765 766 767 768 769
```


### [Symbol.iterator]

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

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

W
wusongqing 已提交
770 771
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
772 773 774 775 776 777
**Return value**

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

G
Gloria 已提交
778 779 780 781 782 783 784 785
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
786 787
**Example**

W
wusongqing 已提交
788
```ts
W
wusongqing 已提交
789
let lightWeightSet = new LightWeightSet();
G
Gloria 已提交
790 791
lightWeightSet.add("squirrel");
lightWeightSet.add("sparrow");
W
wusongqing 已提交
792 793 794

// Method 1:
for (let item of lightWeightSet) { 
W
wusongqing 已提交
795
  console.log("value:" + item);
W
wusongqing 已提交
796 797 798 799 800 801
}

// Method 2:
let iter = lightWeightSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
802
  console.log("value:" + temp);
W
wusongqing 已提交
803 804
  temp = iter.next().value;
}
G
Gloria 已提交
805 806 807 808 809
try {
  lightWeightSet[Symbol.iterator].bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
810
```